Wordpress Themes - Bet365 bonus - Wordpress Theme - Business Gifts - Flash Games

PDA

View Full Version : PHP/SQL loop HELP!!! please.


deanshelton1334
Aug 21st 2007, 3:00 pm
im having issues with this little chunk of PHP code...:confused:

This set of code pulls product data from the mySQL database. each product has basic stuff (name, price, brand, model number, etc...) and most importantly a 'row' called "itemcompad" which includes item numbers of other products it is compatible with.
because i can not anticipate EVERY brand name we may eventually carry... when the user clicks on an item to see what it is compatible with.. i have to explode the commas in "itemcompad " and search each item number's BRAND & attach it to its appropriate array and display it after its all neatly packed away...

If i get any feedback it would be appreciated...any serious questions will be responded to quickly.

Thanks!


//find all possible brands

$sql="SELECT DISTINCT itembrand FROM Products ORDER BY itembrand ASC";
$result= mysql_query($sql) or die(mysql_error());
$numrows=mysql_num_rows(mysql_query($sql));
echo "possible:$numrows";


while(($brandrow = mysql_fetch_array($result))&&($x<$numrows)) {
$unibrand=$brandrow['itembrand'];




//check the displayed items' itemcompad row for ALL brand names and assign them to a unique brand array
$sql="SELECT * FROM Products where itemnumber='$itemnumber'";
$result= mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result)){


$compad = explode(',',$row['itemcompad']);
foreach($compad as $itemquery){

$query = "SELECT * FROM Products WHERE itemnumber ='$itemquery'";
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_fetch_array($result);
$q_itembrand=$rows['itembrand'];
$q_itemnumber=$rows['itemnumber'];
$q_itemmodel= $rows['itemmodelnumber'];

//check for items in the history with the same brand name as the array, and store with other pertnant information.
if($q_itembrand==$unibrand){$combodata[]="$q_itemnumber#$q_itembrand#$q_itemmodel";}


//done! items stored in array. now blow it back up and display it...
$datachunks = explode('#',$combodata);
$datachuncks[1]=$chunck_itemnumber;
$datachuncks[2]=$chunck_itembrand;
$datachuncks[3]=$chunck_itemmodel;
echo"<tr><td> $unibrand:"; if($chunck_itembrand == $unibrand){echo "compad with:$chunck_itemmodel";
}}}}}


WHY WONT THIS WORK!? Please.. Thank you for any Help.. be kind, im New!

deanshelton1334
Aug 21st 2007, 3:08 pm
Im sure there is someone here who can figure this out!

Kuldeep1952
Aug 21st 2007, 5:17 pm
1) Perhaps there should be a assignment statement
somewhere for itemnumber before its use in the query

$itemnumber = $brandrow['itemnumber'];

2) It may be a good idea to have different variables for nested results

$sql="SELECT DISTINCT itembrand FROM Products ORDER BY itembrand ASC";
$result= mysql_query($sql) or die(mysql_error());

$sql1="SELECT * FROM Products where itemnumber='$itemnumber'";
$result1= mysql_query($sql) or die(mysql_error());

$query = "SELECT * FROM Products WHERE itemnumber ='$itemquery'";
$result2 = mysql_query($query) or die(mysql_error());

3) Try echoing mysql_num_rows and values of $sql for each query.

Hope it helps

deanshelton1334
Aug 21st 2007, 8:03 pm
Actually i did define that variable earlier in this script. but thanks for the good advice so far... I also tried echoeing each query... the thing is, they all work...except the code only trys the end variables against ONE unique brand name... the last one... its strange. if i change the first query to DESC insted of ASC the end result is diffrent but still only singular....


1) Perhaps there should be a assignment statement
somewhere for itemnumber before its use in the query

$itemnumber = $brandrow['itemnumber'];

2) It may be a good idea to have different variables for nested results

$sql="SELECT DISTINCT itembrand FROM Products ORDER BY itembrand ASC";
$result= mysql_query($sql) or die(mysql_error());

$sql1="SELECT * FROM Products where itemnumber='$itemnumber'";
$result1= mysql_query($sql) or die(mysql_error());

$query = "SELECT * FROM Products WHERE itemnumber ='$itemquery'";
$result2 = mysql_query($query) or die(mysql_error());

3) Try echoing mysql_num_rows and values of $sql for each query.

Hope it helps