Jump to content

PHP while help


johnrb87

Recommended Posts

Hi

 

I have a QUERY which works fine and looks like;

 

<?php
$result = mysql_query("SELECT products FROM myitems WHERE special = 5");
while($row = mysql_fetch_array($result))
{
echo $row['product'];
echo "<br>";
echo $row['code'];
echo "<br>";
echo $row['price'];
echo "<br><br>";
}
?>

 

This outputs something like

 

iPod, Zen, MpMan

AH729

19.99

 

iMac, HP, VAIO

WD177

899.99

 

HP, Cannon, Xerox

CC223

250.00

 

Which is all great and work fine

 

What I want to do is class each comma separated value as a different row and enclose it with a href tag, so it would basically output

 

<a href="?code=AH729&cost=19.99">iPod</a>

<a href="?code=AH729&cost=19.99">Zen</a>

<a href="?code=AH729&cost=19.99">MpMan</a>

<a href="?code=WD177&cost=899.99">iMac</a>

<a href="?code=WD177&cost=899.99">HP</a>

<a href="?code=WD177&cost=899.99">VAIO</a>

<a href="?code=CC223&cost=250.00">HP</a>

<a href="?code=CC223&cost=250.00">Cannon</a>

<a href="?code=CC223&cost=250.00">Xerox</a>

 

but if possible I would like to output this randomly in any specific order

 

I did think of a  multidimensional array, but have no clue where to start

 

Can anyone help?

 

Thanks very much

Link to comment
https://forums.phpfreaks.com/topic/229511-php-while-help/
Share on other sites

Something like this?

<?php
$result = mysql_query("SELECT products FROM myitems WHERE special = 5");
$tmp = array();
while($row = mysql_fetch_array($result))
{
$products = explode(',',$row['product']);
foreach ($products as $product) {
	$tmp[] = "<a href='?code={$row['code']}&cost={$row['cost']}'>$product</a>";
}
}
shuffle($tmp);
echo implode("<br>\n",$tmp);
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/229511-php-while-help/#findComment-1182465
Share on other sites

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.