Jump to content

array help


searls03

Recommended Posts

this code may look pretty bad, but I need help with the id foreach loop in this code:

<?php 
include_once "connect_to_mysql_1.php";
$target = "images/"; 
$ok=1; 
$name=$_POST['uploaded'];
  $title=$_POST['title'];
  $id=array($_POST['id']);



// Where the file is going to be placed
$target_path = "images/";

foreach ($_FILES["uploaded"]["name"] as $key => $value) 

{


$uploadfile = $target_path . basename($_FILES["uploaded"]["name"][$key]);//echo $uploadfile;
if($value==""){echo "no changes made";
}else{if (move_uploaded_file($_FILES['uploaded']['tmp_name'][$key], $uploadfile)) { echo $value . ' uploaded<br>';	foreach ($id as $key => $value3){

$sql = "REPLACE INTO products SET link='../admin/$target$value',  name='$value', posit='$title', id='$value3'";
$rs = mysql_query($sql) or die ("Problem with the query: $sql<br>" . mysql_error());
	echo mysql_error();}
}
}
}

?> 

 

this is where it is pulling info from:

<form action="upload.php" method="post" enctype="multipart/form-data">
<?php $sql = mysql_query("SELECT id, link FROM products WHERE id>='$basenumber' limit 8");
while($row = mysql_fetch_array($sql)){
$imageid = $row['id'];	
$image2 = $row['link'];
$id = $row['id'];


?>
<div class="imageholder" ><div id="apDiv<?php echo $imageid; ?>" style="background-color:#999; height: 250px; width: 300;"  >

    <input type="text" name="id[]" id="id[]" value="<?php echo $id; ?>"/>
<br />
    <label for="posit">Title:</label>
    <input type="text" name="title[]" id="title[]" />
  <br /><br />
    <label for="uploaded">Picture:</label>
    <input type="file" name="uploaded[]" id="uploaded[]"  />
    <br />
    <br /><center><Div><img src="<?php echo $image2;?>" width="100"/></div></center>

</div>


</div>



<?php } ?>
</div>
<?php
//Adding this after the closing bracket of the array should assign the last image id to the variable $imageid.

if($sql>={
//Get last product id
$getlastproductid = mysql_query("SELECT id FROM products ORDER BY id DESC limit 1");
while($lproductid = mysql_fetch_array($getlastproductid)){
$lastid=$lproductid['id'];
}
echo "<div class=\"paging\">";

$backbasenumber=$basenumber-8;
if($basenumber>8 && $backbasenumber>2){
$backbasenumber=$basenumber-8;
}
else{
$backbasenumber=2;
}
echo "<a href=\"mysite.com/thispage.php?basenumber=$backbasenumber\">Back</a>  ";
}
$nextbasenumber=$imageid+8;
if($nextbasenumber<$lastid){
echo "<a href=\"mysite.com/thispage.php?basenumber=$nextbasenumber\">Next</a>";
}
?><input name="submit" type="submit" /></form>

so I need help so that the query will replace into the database if the id is already there.....or insert if it is not.  the id is being pulled from a loop though, then I need it to submit so that the id that is associated with the picture is what is looked up in the database.  I am bad at arrays and have a hard time figuring them out.  this is hard to explain, hopefully you get what I mean....

Link to comment
Share on other sites

I hope this is a little bit better.  I had changed it a little bit.  here is where code is pulling from:

<form action="upload.php" method="post" enctype="multipart/form-data">
<?php $sql = mysql_query("SELECT id, link FROM 			products WHERE id>='$basenumber' limit 8");
while($row = mysql_fetch_array($sql)){
$imageid = $row['id'];	
$image2 = $row['link'];
$id = $row['id'];


?>
<div class="imageholder" >
<div id="apDiv<?php echo $imageid; ?>" style="background-color:#999; height: 250px; width: 300;"  >

    <input type="text" name="id[]" id="id[]" value="<?php echo $id; ?>"/>
<br />
    <label for="posit">Title:</label>
    <input type="text" name="title[]" id="title[]" />
  <br />
  <br />
    <label for="uploaded">Picture:</label>
    <input type="file" name="uploaded[]" id="uploaded[]"  />
    <br />
    <br />
  <center>
    <Div>
        <img src="<?php echo $image2;?>" width="100"/>
    </div>
  </center>

</div>


</div>

 

 

Yes the form closes later on.  here is the submition code:

<?php 
include_once "connect_to_mysql_1.php";
$target = "images/"; 
$ok=1; 
$name=$_POST['uploaded'];



// Where the file is going to be placed
$target_path = "images/";

foreach ($_FILES["uploaded"]["name"] as $key => $value)  
foreach($_POST['id'] as $id)
{
$uploadfile = $target_path . basename($_FILES["uploaded"]["name"][$key]);//echo $uploadfile;
if (move_uploaded_file($_FILES['uploaded']['tmp_name'][$key], $uploadfile)) { 
echo $value . ' uploaded<br>';	
foreach ($_POST["id"] as $id) {
$sql = "REPLACE INTO products SET id='$id', link='../admin/$target$value',  name='$value', posit='$value4'";
$rs = mysql_query($sql) or die ("Problem with the query: $sql<br>" . mysql_error());
	echo mysql_error();
}
}
}


?> 

 

 

Link to comment
Share on other sites

let me try reexplaining this.  I need to have two loops running that are associated with each other.....they will put info into a database according to id.  the first is the link to the file, the second is the id.  I am using a replace into statement.  I need the code to run loops and put things in according to id.  you can see where this info is coming from inside code.  any help is greatly appreciated.

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.