Jump to content


Photo

1 result given - do i need an array?


  • Please log in to reply
4 replies to this topic

#1 adam_gardner

adam_gardner
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 26 April 2006 - 11:50 AM

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 -

<?

$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>";

?>


#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 26 April 2006 - 11:55 AM

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

<?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++;
}
?>

wisewood: proven fact, I am both wise, and wooden.

#3 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 26 April 2006 - 12:00 PM

<?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>";
?>

hth
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#4 adam_gardner

adam_gardner
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 26 April 2006 - 12:23 PM

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

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 April 2006 - 01:21 PM

I know you said it was solved, but here's another way of doing the same thing...
<?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>';
?>
You'll notice:
  • 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.
Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users