Jump to content

1 result given - do i need an array?


adam_gardner

Recommended Posts

hi guys,

i've simplified my query down to make it easier to understand, but the query's working fine its just hte output.

ok, selecting products, then looping to get all of them, then $main stores the values of the products.

however, when I try and output to the page (within a marquee) it only picks up the first result.

do i need to store the results as an array first? how do i do that?

note :: when i move the "echo - marquee" statement inside the loop it display all products, but each in their own marqee.

What i want to do is make all the products scroll in the marquee.

Any help would be great!

code is below -

[code]
<?

$queryF = "SELECT * FROM Products";

$result=mysql_query($queryF) or die (mysql_error() . "\nActual query: " . $query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

$ProductName=mysql_result($result,$i,"ProductName");

$main = "Products-Returned-From-Result";

$i++;
}

echo "<MARQUEE BEHAVIOUR=SCROLL>$main</MARQUEE>";

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/8452-1-result-given-do-i-need-an-array/
Share on other sites

Put the marquee into the while loop. This way, each result will get its own marquee.

[code]
<?php
$queryF = "SELECT * FROM Products";

$result=mysql_query($queryF) or die (mysql_error() . "\nActual query: " . $query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
$ProductName=mysql_result($result,$i,"ProductName");

$main = "Products-Returned-From-Result";
echo "<MARQUEE BEHAVIOUR=SCROLL>$main</MARQUEE>";
$i++;
}
?>
[/code]
[code]
<?php
$queryF = "SELECT * FROM Products";

$result=mysql_query($queryF) or die (mysql_error() . "\nActual query: " . $query);
$num=mysql_numrows($result);
$main = "";
$i=0;
while ($i < $num) {
$ProductName=mysql_result($result,$i,"ProductName");

$main .= $ProductName." ";
$i++;
}
echo "<MARQUEE BEHAVIOUR=SCROLL>$main</MARQUEE>";
?>
[/code]

hth
I know you said it was solved, but here's another way of doing the same thing...
[code]<?php
$queryF = "SELECT * FROM Products";

$result=mysql_query($queryF) or die (mysql_error() . "\nActual query: " . $query);
$tmp = array();
while ($rw = mysql_fetch_assoc($result))
     $tmp[] = $rw['ProductName'];
if (!empty($tmp) echo '<MARQUEE BEHAVIOUR=SCROLL>' . implode(' ',$tmp) . '</MARQUEE>';
?>[/code]
You'll notice:[list][*]The code is shorter and cleaner.[*]The while loop uses the mysql_fetch_assoc() function which returns an associative array keyed by your field names.[*]I use a temporary array to store the ProductNames[*]I only echo the marquee tag if the array isn't empty. i.e. there are products to display[*]The string to display is formed with the implode() function which, in this case, puts a space between each element of the temporary array.[/list]
Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.