Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function, return and while loop problem

Status
Not open for further replies.

martinb7

Programmer
Jan 5, 2003
235
GB
Hi, i have this function which only returns one row from the table. why??

if i use echo() it returns all, return doesn't?? why? how can i get round it?

this doesn't work
Code:
function L1(){
	
	$query = mysql_query("SELECT * FROM wrv3pages WHERE section = 'main'");
		while($rq = mysql_fetch_array($query)){
			return($rq[&quot;title&quot;]).&quot;<br>&quot;;
		}
}

this works:
function L1(){
	
	$query = mysql_query(&quot;SELECT * FROM wrv3pages WHERE section = 'main'&quot;);
		while($rq = mysql_fetch_array($query)){
			echo($rq[&quot;title&quot;]).&quot;<br>&quot;;
		}
}

any ideas?

thanx


Martin

Computing help and info:

 
As soon as a function see's return, it leaves the function. So after the first row, it returns the value and teh function is done.
 
You create an array inside your function, push your values to the end of the array, then return the array:

Code:
function L1()
{
   $return_val = array();
   $query = mysql_query(&quot;SELECT * FROM wrv3pages WHERE section = 'main'&quot;);
   while($rq = mysql_fetch_array($query))
   {
      $return_val[] = $rq['title'];
   }
   return $return_val;
}

Want the best answers? Ask the best questions: TANSTAAFL!!
 
it said the eror is on line 39 but on line 39 the code is $var .=&quot; </tr>&quot;;
so i dont no wot to do??

any more ideas?


Martin

Computing help and info:

 
it didn't return a parse error! it returned the Notice: Array to string conversion.i could email you the file if you want to look at it?



Martin

Computing help and info:

 
What about using Array_puch function:
Code:
function L1()
{
   $return_val = array();
   $query = mysql_query(&quot;SELECT * FROM wrv3pages WHERE section = 'main'&quot;);
   while($rq = mysql_fetch_array($query))
   {
      array_push($return_val[], $rq['title']);
   }
   return $return_val;
}
 
Sorry, Should have checed the code before posting. The function should look like:
Code:
function L1()
{
   $return_val = array();
   $query = mysql_query(&quot;SELECT * FROM wrv3pages WHERE section = 'main'&quot;);
   while($rq = mysql_fetch_array($query))
   {
      array_push($return_val, $rq['title']);
   }
   return $return_val;
}
 
here is the whole code! il highlight the line that says to be an error (
Code:
Notice: Array to string conversion in c:\inetpub\[URL unfurl="true"]wwwroot\webrevoltV3\test\index.php[/URL] on line 39
)

Code:
<?
include(&quot;config.php&quot;);
$page = $_GET['p'];

/*function doWhileLoop($query){
	while($r = mysql_fetch_array($query)){
		//$lu = $r[&quot;pagename&quot;];
		//$lu2 = $r[&quot;pagename2&quot;];
		$t = $r[&quot;title&quot;];
		$link = $r[&quot;link&quot;];
		return(&quot;<a href=$link>$t</a>&quot;)
		//return($pr);
	}
}*/

function titlebar(){
  	return(&quot;<img src=seasons/normal/titlebar.gif>&quot;);
}
function left(){

$var = &quot;	<table width=150 border=0 cellpadding=0 cellspacing=0>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;    <td height=13 valign=top class=nav><div align=center>Main Pages</div></td>&quot;;
$var .=&quot;  	</tr>&quot;;
$var .=&quot;  	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=titlebar><div align=center>[## l1 ##] </div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=nav><div align=center>Computing</div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=15 valign=top class=titlebar><div align=center>[## l2 ##] </div></td>&quot;;
$var .=&quot;    </tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=nav><div align=center>Community</div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=titlebar><div align=center>[## l3 ##] </div></td>&quot;;
[COLOR=#ff0000]$var .=&quot;	</tr>&quot;; //this seems to be the error that it says about - this is line 39[/color]
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=18 valign=top class=nav><div align=center>Other</div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=22 valign=top class=titlebar><div align=center>[## l4 ##] </div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	</table>&quot;;

return($var);
} 
function right(){

$var = &quot;	<table width=150 border=0 cellpadding=0 cellspacing=0>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;    <td height=13 valign=top class=nav><div align=center>Stats</div></td>&quot;;
$var .=&quot;  	</tr>&quot;;
$var .=&quot;  	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=titlebar><div align=center>[## r1 ##] </div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=nav><div align=center>Poll</div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=15 valign=top class=titlebar><div align=center>[## r2 ##] </div></td>&quot;;
$var .=&quot;    </tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=nav><div align=center>Login</div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=13 valign=top class=titlebar><div align=center>[## r3 ##] </div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=18 valign=top class=nav><div align=center>Other</div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	<tr>&quot;;
$var .=&quot;	<td height=22 valign=top class=titlebar><div align=center>[## r4 ##] </div></td>&quot;;
$var .=&quot;	</tr>&quot;;
$var .=&quot;	</table>&quot;;

return($var);
} 
function content(){
 	$query = mysql_query(&quot;SELECT * FROM wrv3pages WHERE title = '&quot;.$_GET['p'].&quot;'&quot;) or die(mysql_error());
	while($rq = mysql_fetch_array($query)){
		return(nl2br($rq[&quot;pageinfo&quot;])).&quot;<br>&quot;;
	}
}
function copyright(){
  	return(&quot;© 2002 - &quot;.date(&quot;Y&quot;). &quot; WebRevolt&quot;);
}
function sitecss(){
	return(&quot;<link href=site.css rel=stylesheet type=text/css>&quot;);
}

#### Nav Bars ####
function L1()
{
$return_val = array();
$query = mysql_query(&quot;SELECT * FROM wrv3pages WHERE section = 'main'&quot;);
while($rq = mysql_fetch_array($query))
{
array_push($return_val, $rq['title']);
}
return $return_val;
} 

function L2(){
	return(&quot;Computing&quot;);
}
function L3(){
	return(&quot;Community&quot;);
}
function L4(){
	return(&quot;Other&quot;);
}
#### Right ####
function R1(){
	return(&quot;Stats&quot;);
}
function R2(){
	return(&quot;Coming Soon&quot;);
}
function R3(){
	return(&quot;Login Info&quot;);
}
function R4(){
	return(&quot;Other&quot;);
}
?>

Martin

Computing help and info:

 
Westbury:
This line:

array_push($return_val, $rq['title']);

and this line:

$return_val[] = $rq['title'];

are functionally equivalent.


martinb7:
That error message doesn't make sense on that line.

Try this: instead of using include() on config.php, typographically insert that file's content into the offending script. Rerun the script and see what happens.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sleipnir214
I wasn't aware of that little shortcut. I guess you do learn something new everyday. :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top