SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    editing items in database in a loop from post array?

    I am creating the post in the following form:

    PHP Code:
    while ( $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']."]\" id=\"trackname[".$tracks['song_id']."]\" value=\"".$tracks['song_title']."\"/> ID:".$tracks['song_id']."</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=14

    I can see all the data there, but I'm not really sure how to manipulate it... I'm not so hot with arrays.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would prepare the form like this:
    PHP Code:
    <?php while($tracks mysql_fetch_array($trackstring)): ?> 
        <li class="tracklist">
            <input type="text" name="tracknum[<?php echo $tracks['song_id'];?>]" id="tracknum_<?php echo $tracks['song_id']; ?>" value="<?php echo $tracks['song_tracknumber'];?>" />  
            <input type="text" name="trackname[<?php echo $tracks['song_id']; ?>" id="trackname_<?php echo $tracks['song_id']; ?>" value="<?php echo $tracks['song_title']; ?>" /> 
            ID: <?php echo $tracks['song_id']; ?>
        </li> 
    <?php endwhile; ?>
    Note the id of each form element in the array which should be unique not the array.

    Then in the submit script will be like this:
    PHP Code:
    $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";
        }

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is awesome! does just what I need . thank you so much. I'd actually been struggling with that on and off for a few days (fairly new to php)

    the part I was missing from my php knowledge was
    foreach($tracknum as $songid=>$tracknumber)
    never actually done a foreach.
    what would I have to do to that line to include a third option? say, $tracklength?

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the code that generates your markup duplicate the line with the <input> and change the references to tracklength.
    Then in the code that updates the DB add $tracklength = $_POST['tracklength'] and add the extra field to your update query. Just follow the template of what is there, but access $tracklength.

    Also you need to escape the input. Even if you only use this system yourself you'll want the ability for track names to have special characters.
    PHP Code:
    $tracknum $_POST['tracknum'];
    $trackname $_POST['trackname'];
    $tracklength $_POST['tracklength'];

    if(
    count($tracknum) == count($trackname)){ // make sure both the array have same elements
        
    foreach($tracknum as $songid=>$tracknumber){
            
    $song_tracknumber = (int)$tracknum[$songid];
            
    $song_title mysql_real_escape_string(trim($trackname[$songid]));
            
    $song_length mysql_real_escape_string(trim($tracklength[$songid]));

            
    $sql "UPDATE songs SET song_tracknumber='$song_tracknumber', song_title='$song_title', song_length='$song_length' WHERE song_id=$songid";
        }

    The song_length field of your DB should probably be a time based format (rather than varchar) so you can sort/order by it if necessary.

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok great!
    so the "foreach($tracknum as $songid=>$tracknumber){" wouldn't have to change?
    that's what I was wondering about

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    These are known as parallel arrays. The 2 arrays (3 with tracklength) have elements with the same keys and each contains its own piece of data (track number, name, length).

    Because they are 'parallel' you could do the foreach loop on any of them. The $songid in the loop is the array key for all of the arrays. So you can use it to access the corresponding value for as many arrays as you wish to create.

    As an aside your HTML could be structured like this to have everything in one multidimensional array. You don't need to change, this is just FYI:
    Code HTML:
    <!-- track ID 12 -->
    Track Number: <input type='text' name='track_data[12][track_num]' maxlength='3'>
    Track Name: <input type='text' name='track_data[12][track_name]'>
    Track Length: <input type='text' name='track_data[12][track_length]'>
     
    <!-- track ID 18 -->
    Track Number: <input type='text' name='track_data[18][track_num]' maxlength='3'>
    Track Name: <input type='text' name='track_data[18][track_name]'>
    Track Length: <input type='text' name='track_data[18][track_length]'>

    And to iterate the data in PHP
    PHP Code:
    foreach($_POST['track_data'] as $songid => $data) {
        
    $songid = (int)$songid;
        
    $num = (int)$data['track_num'];
        
    $name mysql_real_escape_string(trim($data['track_num']));
        
    $length mysql_real_escape_string(trim($data['track_length']));
        
        
    $sql "UPDATE songs SET song_tracknumber=$num, song_title='$name', song_length='$length' WHERE song_id=$songid";



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •