Jump to content

Indexing through an array


Bellage

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/157724-indexing-through-an-array/
Share on other sites

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);


?>

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);


?>

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.