Jump to content

Foreach Not Returning First Value In Array


sac0o01

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.