Akenatehm Posted February 7, 2009 Share Posted February 7, 2009 Hey Guys, I am trying to make a script that will repeat a block of code for each entry in the database and fill each one with the fields of the respective rows. Here's the part of the code that I think is neccessary: <?php $getitems = mysql_query("SELECT itemid FROM rewards"); if(!$getitems) { die('Failed to Get Items') . mysql_error(); } else { while($items = mysql_fetch_array($getitems, MYSQL_ASSOC)) { $allitems = $items['itemid']; } $searchitems = mysql_query("SELECT * FROM items WHERE entry = '$allitems'"); if(!$searchitems) { die('Could Not Search for Items' . mysql_error()); } else { while($allrows = mysql_fetch_array($searchitems, MYSQL_ASSOC)) { $numrows = mysql_num_rows($searchitems); $name1 = $allrows['name1']; $displayid = $allrows['displayid']; $requiredlevel = $allrows['requiredlevel']; $unique = $allrows['Unique']; $quality = $allrows['quality']; $description = $allrows['description']; } $i = 0; while ($i < $num){ echo "Name: $name1<br /> Display ID:$displayid<br /> Required Level: $requiredlevel <br /> Unique: $unique <br /> Quality: $quality <br /> Description: $description <br /> "; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/ Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 So uh... What exactly is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757091 Share on other sites More sharing options...
Cosizzle Posted February 8, 2009 Share Posted February 8, 2009 Could you not put your echo "Name: $name1<br /> Display ID:$displayid<br /> Required Level: $requiredlevel <br /> Unique: $unique <br /> Quality: $quality <br /> Description: $description <br /> "; Into the loop thats grabbing the info? while($allrows = mysql_fetch_array($searchitems, MYSQL_ASSOC)) { $numrows = mysql_num_rows($searchitems); $name1 = $allrows['name1']; $displayid = $allrows['displayid']; $requiredlevel = $allrows['requiredlevel']; $unique = $allrows['Unique']; $quality = $allrows['quality']; $description = $allrows['description']; echo "Name: $name1<br /> Display ID:$displayid<br /> Required Level: $requiredlevel <br /> Unique: $unique <br /> Quality: $quality <br /> Description: $description <br /> "; } Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757093 Share on other sites More sharing options...
Akenatehm Posted February 8, 2009 Author Share Posted February 8, 2009 @corbin Well. It is only showing one record. @Cosizzle The while loop it is in is counting how many times it needs to re write it. So I don't know how to transfer that across. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757102 Share on other sites More sharing options...
Akenatehm Posted February 8, 2009 Author Share Posted February 8, 2009 Anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757120 Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 Try this: $allitems = array(); while($items = mysql_fetch_array($getitems, MYSQL_ASSOC)) { $allitems[] = $items['itemid']; } $items = implode(', ', $allitems); $searchitems = mysql_query("SELECT * FROM items WHERE entry IN( $allitems)"); And see if that solves your issue. Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757132 Share on other sites More sharing options...
Akenatehm Posted February 8, 2009 Author Share Posted February 8, 2009 Here's the code in the area that's not working: $getitems = mysql_query("SELECT itemid FROM rewards"); if(!$getitems) { die('Failed to Get Items') . mysql_error(); } else { $allitems = array(); while($items = mysql_fetch_array($getitems, MYSQL_ASSOC)) { $allitems[] = $items['itemid']; } $items = implode(', ', $allitems); $searchitems = mysql_query("SELECT * FROM items WHERE entry IN( $allitems)"); if(!$searchitems) { die('Could Not Search for Items' . mysql_error()); After changing what you suggeted I now get these errors: Could Not Search for ItemsUnknown column 'Array' in 'where clause' Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757148 Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 Sorry, small change: $allitems = array(); while($items = mysql_fetch_array($getitems, MYSQL_ASSOC)) { $allitems[] = $items['itemid']; } $allitems = implode(', ', $allitems); // changed this to allitems $searchitems = mysql_query("SELECT * FROM items WHERE entry IN( $allitems)"); Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757155 Share on other sites More sharing options...
Akenatehm Posted February 8, 2009 Author Share Posted February 8, 2009 Ok, I changed that. Now the page just shows up blank. Code Update: <?php $host = "localhost"; $name = "username"; $pass = "pass"; $db = "db"; $con = mysql_connect($host,$name,$pass); if (!$con) { die('Failed to Connect:' . mysql_error()); } $selectdb = mysql_select_db($db); if (!$selectdb) { die('Failed to Select Database:' . mysql_error()); } $getitems = mysql_query("SELECT itemid FROM rewards"); if(!$getitems) { die('Failed to Get Items') . mysql_error(); } else { $allitems = array(); while($items = mysql_fetch_array($getitems, MYSQL_ASSOC)) { $allitems[] = $items['itemid']; } $allitems = implode(', ', $allitems); // changed this to allitems $searchitems = mysql_query("SELECT * FROM items WHERE entry IN( $allitems)"); if(!$searchitems) { die('Could Not Search for Items' . mysql_error()); } else { while($allrows = mysql_fetch_array($searchitems, MYSQL_ASSOC)) { $numrows = mysql_num_rows($searchitems); $name1 = $allrows['name1']; $displayid = $allrows['displayid']; $requiredlevel = $allrows['requiredlevel']; $unique = $allrows['Unique']; $quality = $allrows['quality']; $description = $allrows['description']; } $i = 0; while ($i < $num){ echo "Name: $name1<br /> Display ID:$displayid<br /> Required Level: $requiredlevel <br /> Unique: $unique <br /> Quality: $quality <br /> Description: $description <br /> "; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757175 Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 You are not setting $num to be anything for the last while statement. Since it would be looked at as "0" this is never ran. What is $num support to be? Also $description = $allrows['description']; Each time the loop is ran that is overwritten. Why not move the data from the third loop into the second... while($allrows = mysql_fetch_array($searchitems, MYSQL_ASSOC)) { $numrows = mysql_num_rows($searchitems); $name1 = $allrows['name1']; $displayid = $allrows['displayid']; $requiredlevel = $allrows['requiredlevel']; $unique = $allrows['Unique']; $quality = $allrows['quality']; $description = $allrows['description']; echo "Name: $name1<br /> Display ID:$displayid<br /> Required Level: $requiredlevel <br /> Unique: $unique <br /> Quality: $quality <br /> Description: $description <br /> "; } Give that a try and see what comes of it. Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757181 Share on other sites More sharing options...
Akenatehm Posted February 8, 2009 Author Share Posted February 8, 2009 Sweet, thanks very much. Can you show me in comments what all these new editions are doing. I like to learn how things work. It works great.!! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757184 Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 The first, since you were just dong $allitems = $rowentry you were assigning that a new variable each time. You expected it to keep the results, but the = is an assignment operator, so it was being overwritten. What I did there was make $allitems and array so we could store each entry as an element of an array then I used the implode outside of the loop to combine the elements of an array with a , spliiting them. This was because the IN function in my sql can take multiple items as long as they are split by a comma. This allowed for the search to actually works, since you will not just be searching the last item obtained from the first sql, instead it pulled where all the IDs were in the $allitems. Next, moving the echo inside the first while, is more or less just common coding. Why have 2 loops when one will suffice. Since you were not doing anything else with the return from the DB but echoing it was better/easier just to put that inside that while loop. Quote Link to comment https://forums.phpfreaks.com/topic/144264-repeating-code-for-each-entry-in-the-sql-database/#findComment-757188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.