vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   calling variable from another file (https://vborg.vbsupport.ru/showthread.php?t=126804)

error_22 09-17-2006 01:02 AM

calling variable from another file
 
Hi,

I was wondering if someone could explain something to me. I have a file called news.php:

news.php
PHP Code:

<?
$sql = "SELECT * FROM `p_news` ORDER BY `p_id` DESC";  
$result = mysql_query($sql) or die(mysql_error());  
while ($row = mysql_fetch_object($result))  
{      
$title = $row->p_title;
$content = $row->p_content;
}
?>

Then I have another file, index.php:

index.php
PHP Code:

<?
require ("./includes/db_con.php"); //connects to DB
require ("./includes/news.php");

echo $title;
echo "<br />";
echo $content;
echo "<br /><br />";
?>

My problem is, only the first row shows up, why is that, and how do I fix it?

Thanks in advance
Niklas

Guest190829 09-17-2006 01:52 AM

Quote:

Originally Posted by error_22
Hi,

I was wondering if someone could explain something to me. I have a file called news.php:

news.php
PHP Code:

<?


require ("./includes/db_con.php"); //connects to DB

$sql = "SELECT * FROM `p_news` ORDER BY `p_id` DESC";  
$result = mysql_query($sql) or die(mysql_error());  
while ($row = mysql_fetch_object($result))  
{      
$title = $row->p_title;
$content = $row->p_content;

echo $title;
echo "<br />";
echo $content;
echo "<br /><br />";

}
?>

Then I have another file, index.php:

index.php
PHP Code:

<?
require ("./includes/news.php");

echo $title;
echo "<br />";
echo $content;
echo "<br /><br />";
?>

My problem is, only the first row shows up, why is that, and how do I fix it?

Thanks in advance
Niklas

Because that while loop is already processed before you echo $title and $content...

If you make it like this:

PHP Code:

<?


 require ("./includes/db_con.php"); //connects to DB

$sql = "SELECT * FROM `p_news` ORDER BY `p_id` DESC";  
$result = mysql_query($sql) or die(mysql_error());  
while ($row = mysql_fetch_object($result))  
{      
$title = $row->p_title;
$content = $row->p_content;

 echo $title;
 echo "<br />";
 echo $content;
 echo "<br /><br />";

}
?>

It should work...

error_22 09-17-2006 02:27 AM

The thing is, the content of the "news" table is supposed to be displayed in more than one file, and the content is meant to be displayed differently on different pages. Do I really need to have a new query in every new file? isnt there an easier solution?


All times are GMT. The time now is 09:25 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01047 seconds
  • Memory Usage 1,723KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (3)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete