Bellage Posted May 11, 2009 Share Posted May 11, 2009 Hello everyone! What I am trying to achieve is probably the most basic of things but I am new to this and have been struggling to find a good explanation or at least a layman enough explanation so that I can understand it. I am creating an array with several figures in. I then want to output each entry from the array, one at a time and in order of appearance, within a loop and to finish looping when the end of the array has been reached. $entries = array('17957','17962','23452','45235','54345'); while($number=$entries) //some sort of loop condition { echo $number; } So basically it would print to screen 17957 then loop and print 17962 and so on then exit the loop once we get to the end of the array. Anyhow sorry if my explanation of what I want is little shady, but any help appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/ Share on other sites More sharing options...
Daniel0 Posted May 11, 2009 Share Posted May 11, 2009 foreach ($entries as $entry) { echo $entry; } Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/#findComment-831868 Share on other sites More sharing options...
jackpf Posted May 11, 2009 Share Posted May 11, 2009 foreach is just the thing for you ...oh. Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/#findComment-831869 Share on other sites More sharing options...
Bellage Posted May 11, 2009 Author Share Posted May 11, 2009 That seems great. So if I use foreach under these circumstances I would have no problems? <?php $root = './'; include($root . 'con_conf.php'); $connect = mysql_connect($zdbhost, $zdbuser, $zdbpass) or die ('Database Connection Error!'); $entries = array('17957','17962'); foreach ($entries as $normalentry) { $Nfields = "SELECT `baseattacktime`, `rangeattacktime`, `rangedattackpower`, `heroic_entry`, `unit_flags`, `lootid`, `pickpocketloot`, `equipment_id`, `MovementType` FROM `mangos`.`creature_template` WHERE `entry` = '.$normalentry.'"; $Nfieldsresult = mysql_db_query($Nfields) or die ("Failed to Gather Normal Fields"); $Nrow = mysql_fetch_row($Nfieldsresult); $Hupdate= "UPDATE `mangos`.`creature_template` SET `baseattacktime` = '.$Nrow[0].', `rangeattacktime` = '.$Nrow[1].', `rangedattackpower` = '.$Nrow[2].', `unit_flags` = '.$Nrow[4].', `lootid` = '.$Nrow[5].', `pickpocketloot` = '.$Nrow[6].', `equipment_id` = '.$Nrow[7].', `MovementType` = '.$Nrow[8].' WHERE `heroic_entry` = '.$Nrow[3].'"; $Hupdateresult = mysql_db_query($Hupdate) or die ("Failed to Update Heroic Fields"); } mysql_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/#findComment-831884 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Not exactly. You should leave out the dots, but I prefer this - <?php $root = './'; include($root . 'con_conf.php'); $connect = mysql_connect($zdbhost, $zdbuser, $zdbpass) or die ('Database Connection Error!'); $entries = array('17957','17962'); foreach ($entries as $normalentry) { $Nfields = 'SELECT `baseattacktime`, `rangeattacktime`, `rangedattackpower`, `heroic_entry`, `unit_flags`, `lootid`, `pickpocketloot`, `equipment_id`, `MovementType` FROM `mangos`.`creature_template` WHERE `entry` = "'.$normalentry.'"'; $Nfieldsresult = mysql_db_query($Nfields) or die ('Failed to Gather Normal Fields'); $Nrow = mysql_fetch_row($Nfieldsresult); $Hupdate= 'UPDATE `mangos`.`creature_template` SET `baseattacktime` = "'.$Nrow[0].'", `rangeattacktime` = "'.$Nrow[1].'", `rangedattackpower` = "'.$Nrow[2].'", `unit_flags` = "'.$Nrow[4].'", `lootid` = "'.$Nrow[5].'", `pickpocketloot` = "'.$Nrow[6].'", `equipment_id` = "'.$Nrow[7].'", `MovementType` = "'.$Nrow[8].'" WHERE `heroic_entry` = "'.$Nrow[3].'"'; $Hupdateresult = mysql_db_query($Hupdate) or die ('Failed to Update Heroic Fields'); } mysql_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/#findComment-831889 Share on other sites More sharing options...
Daniel0 Posted May 12, 2009 Share Posted May 12, 2009 You also shouldn't use or die(). Use some real error handling. At least call trigger_error instead or die(). Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/#findComment-832086 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Daniel0, you mean for mysql_fetch_row? Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/#findComment-832128 Share on other sites More sharing options...
Daniel0 Posted May 12, 2009 Share Posted May 12, 2009 I mean in general. Doing that or die() trick is a horrible way of doing error handling. It can't be caught or logged, and it can't be hidden in a production environment. Quote Link to comment https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/#findComment-832193 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.