Jump to content

Archived

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

adam_gardner

1 result given - do i need an array?

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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
ahh great!

all i was missing was the . after $main

wisewood - i mentioned that i used your method, but i wanted all the results in the same marquee - so they scroll side by side.

all sorted and solve3d now - thanks!


Adam

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.