Forum Moderators: coopster

Message Too Old, No Replies

function return multiple results

         

dkin

5:44 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



This is my function

function display_comments()
{
global $id, $link;

$cresult = mysql_query("SELECT * FROM comments where productid = '$id' order by id asc", $link) or die ("vquery 1: " . mysql_error());
$num_rows = mysql_num_rows($cresult);

if ($num_rows >= 1)
{

echo '<table summary="" border="1">';
while($crow = mysql_fetch_array($cresult))
{

$color = "blue";

if($color == "blue")
{

$color = "red";

##Close if
}

return '<tr bgcolor="'.$color.'"><td>'.$crow['title'].'<br>'.$crow['uname'].'</td></tr><tr bgcolor="'.$color.'"><td>'.$crow['review'].'</td></tr><tr bgcolor="'.$color.'"><td>'.$crow['rating'].'</td></tr>';

##Close while loop
}

echo '</table>';
##Close if
}
else
{

echo 'There are no comments for this item.';

}

}

But even if there are 20 rows it only displays one, how can I get the correct output?

Sarah Atkinson

5:55 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



try looking at this
post
[webmasterworld.com]

dkin

6:05 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



wow this is so much more complicated then I thought, any easier ways?

Sarah Atkinson

6:21 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



your return prints the info then closes the fuction.

Maybe you should try an echo statment there instead.

why do you have this?


while($crow = mysql_fetch_array($cresult))
{
$color = "blue";
if($color == "blue")
{
$color = "red";
##Close if
}

here you are difining blue then saying to change it to red why not just define it as red?

Sarah Atkinson

6:24 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



Also might an include work better? YOu are not passing nor realy returning any values and I also see no reason for globals.

I don't use globals.
I think they are disabled on my server by my service provider anyway.
Sarah

dkin

6:25 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



when I try an echo statement it displays it in a completely different part of the page, I have a certain place I want it displayed.

with the color I wanted it to change everytime a new row was output, so blue, red, blue red but it didnt seem to work as I planned lol.

Sarah Atkinson

6:30 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



I guess you could do something like
$commentarray[$x]='<tr bgcolor="'.$color.'"><td>'.$crow['title'].'<br>'.$crow['uname'].'</td></tr><tr bgcolor="'.$color.'"><td>'.$crow['review'].'</td></tr><tr bgcolor="'.$color.'"><td>'.$crow['rating'].'</td></tr>';

increment the $x each time the while statment cycles

as for the color

$color = "blue";
while($crow = mysql_fetch_array($cresult))
{

blaa blaa blaa

if($color == "blue")
$color = "red";
else
$color= "blue";
}

dkin

6:30 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



I am creating one file which is included in all of my pages, this file will have all my functions in it, this makes it much easier for me.

Sarah Atkinson

6:33 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



oh and at the bottom you would need something like
return $commentarray[];

I think

and then where you call your function

$foo=display_comments();

then you will need something that will print off each of the array values

my best sugestion is to change it to an include file and use echo. if it is printing it in the wrong place then.. your problem lies in where you have your include file.

Sarah