mysql - PHP Nested FOR Loops Troubleshooting -
i have database 10 columns in row, each set contain image name.
himage1, himage2, himage3, etc...
the user can delete of these images individually, , corresponding column value set 'na', signify there no image.
but there 3 images, means 'himage4' onwards fields 'na', , user deletes 'himage1', row this:
himage1 = 'na', himage2 = "...img2.jpg", himage3 = "...im3.jpg", himage4 = 'na', himage5 = 'na', etc...
this creates problem, have column, 'hnumimage', controls how many images set displayed.
but if 'hnumimage' 2, in example, there 2 images, loop use display images still try display 'himage1', though blank.
for reason i'm trying create function detect if field 'na', , check if of following fields have valid image. if so, move image value forward.
so in example above, should after function run:
himage1 = "...img2.jpg", himage2 = "...im3.jpg", himage3 = 'na', himage4 = 'na', himage5 = 'na', etc...
i'm using 2 nested loops, i'm having problems logic involved.
for ( $i = 1; $i <= 10; $i++ ) { // if image field na if ( $rs_home_delete_select_array[ 'himage' . $i ] == na ) { //check following images ( $o = $i + 1; $o <= 10; $o++ ) { // if found image value if ( $rs_home_delete_select_array[ 'himage' . $o ] != na ) { // move value forward, , clear old field $vcurrimgval = $rs_home_delete_select_array[ 'himage' . $o ]; $sql_arrange_img = "update tblhome set himage" . $i . " = '" . $vcurrimgval . "', himage" . $o . " = 'na' hid = 1"; $rs_arrange_img = mysqli_query( $vconn_db, $sql_arrange_img ); } } }
this works, once.
the example above this:
himage1 = "...img3.php", himage2 = 'na', himage3 = 'na', himage4 = 'na', etc
the inner loop succesfully swaps 'himage1' 'himage2', compares 'himage1' 'himage3' after that, replaces 'himage3', leaving other fields blank.
i've identified inner loop running after 2 values have been swapped, i've tried breaking out of inner loop on successful swap, outer loop seems still not work.
i know i'm missing something, , i've been stuck on while.
some insight logic , order involved in issue appreciated.
i think nested loop complicated way of doing this. , have problem array changing while search it. split 2 steps. first of all, data want. rest.
and while you're @ it, it's practice quote string values (so 'na'
instead of na
)
<?php $rs_home_delete_select_array=array('himage1' => 'na', 'himage2' => "...img2.jpg", 'himage3' => "...im3.jpg", 'himage4' => 'na', 'himage5' => 'na'); $temp = array(); $idx = 1; //step 1 put data first ( $o = $i + 1; $o <= 10; $o++ ) { if ( isset($rs_home_delete_select_array[ 'himage' . $o ]) && $rs_home_delete_select_array[ 'himage' . $o ] != 'na' ) { $temp[ 'himage' . $idx ] = $rs_home_delete_select_array[ 'himage' . $o ]; $idx++; } } //step 2. put 'na' elements later ( $o = $i + 1; $o <= 10; $o++ ) { if ( isset($rs_home_delete_select_array[ 'himage' . $o ]) && $rs_home_delete_select_array[ 'himage' . $o ] == 'na' ) { $temp[ 'himage' . $idx ] = $rs_home_delete_select_array[ 'himage' . $o ]; $idx++; } } //update db foreach ($temp $key => $val){ $sql_arrange_img = "update tblhome set `$key` = '$val' hid = 1"; $rs_arrange_img = mysqli_query( $vconn_db, $sql_arrange_img ); } ?>
Comments
Post a Comment