sac0o01 Posted December 12, 2012 Share Posted December 12, 2012 I start out by pulling a list of image urls from the database then I run it through a foreach loop to display the results and change the weight of the image <?php foreach ($image as $image_arr){?> <tr> <td width="70"><center><img src="<?php echo $image_arr;?>" alt="rotation" /><br /> URL:<?php echo $image_arr;?></center> <center> <?php //get image weights $weightresult = mysql_query("SELECT weight FROM images WHERE url ='$image_arr'"); $weightrow = mysql_fetch_row($weightresult); $weightcontents = $weightrow[0]; ?> higher weight images will show more often <br /> <form action= "changeweight.php" method="post"> Weight (1 to 9): <input type="number" name="weight" min="1" max="9" value="<?php echo $weightcontents ?>" /> <input type="hidden" name="seturl" VALUE="<?php echo $image_arr ?>"/> <input type="submit" VALUE="set weight" /> </form> The problem is all the weight boxes work except for the first one. Could the fact that it is [0] in the array be messing it up? Here is changeweight.php if it helps: <?php session_start(); //connect to db include("dbc.php"); $setweight = $_POST['weight']; $seturl = $_POST['seturl']; mysql_query ("UPDATE images SET weight = '$setweight' WHERE url = '$seturl' ; ") or die (mysql_error()); header("Location: /xxxxxx/myaccount.php",TRUE,303); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271894-foreach-not-returning-first-value-in-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2012 Share Posted December 12, 2012 The problem is all the weight boxes work except for the first one What exactly doesn't the first one do? Is it missing entirely from the form? Is the hidden field in the form incorrect? It doesn't update? It produces an error when the update query runs? BTW - you should not execute one query, then loop over the result from that query and execute another query in the loop to just get data the first query should have already selected. Quote Link to comment https://forums.phpfreaks.com/topic/271894-foreach-not-returning-first-value-in-array/#findComment-1398901 Share on other sites More sharing options...
sac0o01 Posted December 12, 2012 Author Share Posted December 12, 2012 Thanks for the response. I'll try to explain a little better. The form displays properly in all of the foreach iterations. However the first iteration does not run changeweight.php when the submit button is clicked. All the other iterations work perfectly. I added shuffle($image) before the loop and when the previous first iteration is no longer first , it updates weight fine. Also, I understand what you mean about the query in the loop but not sure how to implement it outside the loop because I need the $image_arr variable from the foreach statement Quote Link to comment https://forums.phpfreaks.com/topic/271894-foreach-not-returning-first-value-in-array/#findComment-1398914 Share on other sites More sharing options...
kicken Posted December 12, 2012 Share Posted December 12, 2012 What happens when you click the button? Does it do nothing at all, or does it run some other file? Perhaps you have an issue with trying to nest <form> tags. Quote Link to comment https://forums.phpfreaks.com/topic/271894-foreach-not-returning-first-value-in-array/#findComment-1399019 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.