How to pull data from complex array in php?
I am creating the post in the following form:
while ( $tracks = mysql_fetch_array($trackstring) ){
$tracks = mysql_fetch_array($trackstring) ){
echo "<li class="tracklist">
<input type="text" name="tracknum-[$tracks['song_id']]"
value="".$tracks['song_tracknumber']"/>
<input type="text" name="trackname[$tracks['song_id']]"
value="".$tracks['song_title'].""/>
</li>";
}
it's creating a $_POST array that looks like开发者_开发百科 this:
Array ( [tracknum] => Array ( [13] => 1 [14] => 2 [15] => 3 ) [trackname] => Array ( [13] => One Beat [14] => Faraway [15] => Oh! )
essentially I want
1 - One Beat to go into id=13 2 - Faraway to go into id=14I can see all the data there, but I'm not really sure how to manipulate it... I'm not so hot with arrays. How do I reference these with $_POST['????'] involving the id values
use
$_POST['trackname'][13]
to get "One Beat".
Try to use
print_r($_POST);
That will give you your POST clearly indented, so you can easily navigate through it.
And then, edit your question to sound more clear, if you still need help.
big thanks to cranial-bore at the sitepoint forums :)
$tracknum = $_POST['tracknum'];
$trackname = $_POST['trackname'];
if(count($tracknum) == count($trackname)){ // make sure both the array have same elements
foreach($tracknum as $songid=>$tracknumber){
$song_tracknumber = $tracknum[$songid];
$song_title = $trackname[$songid];
$sql = "UPDATE songs SET song_tracknumber='$song_tracknumber', song_title='$song_title' WHERE song_id=$songid";
}
}
精彩评论