searls03 Posted January 5, 2012 Share Posted January 5, 2012 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.... Quote Link to comment https://forums.phpfreaks.com/topic/254439-array-help/ Share on other sites More sharing options...
searls03 Posted January 6, 2012 Author Share Posted January 6, 2012 I think what I am trying to do is write two foreach statements in one statement.... so foreach uploaded and id basically, I need the uploaded to be associated with the id. Quote Link to comment https://forums.phpfreaks.com/topic/254439-array-help/#findComment-1304685 Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 It might be worth cleaning up the line-spacing, and tabbing before someone reviews it. It'd be much easier for us to help you. Quote Link to comment https://forums.phpfreaks.com/topic/254439-array-help/#findComment-1304687 Share on other sites More sharing options...
searls03 Posted January 6, 2012 Author Share Posted January 6, 2012 Ok. When I am at my computer tomorrow I will try.....I'm not the best at makin gods look nice, but with the shortness of it I should be able to....in the mean time if anyone has advice on the issue I will incorporate it as I clean it up.... Quote Link to comment https://forums.phpfreaks.com/topic/254439-array-help/#findComment-1304693 Share on other sites More sharing options...
searls03 Posted January 7, 2012 Author Share Posted January 7, 2012 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(); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254439-array-help/#findComment-1305357 Share on other sites More sharing options...
searls03 Posted January 8, 2012 Author Share Posted January 8, 2012 does what I awant to do make sense? I basicallyt need to write two foreach statements in one......I think..... Quote Link to comment https://forums.phpfreaks.com/topic/254439-array-help/#findComment-1305542 Share on other sites More sharing options...
searls03 Posted January 10, 2012 Author Share Posted January 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254439-array-help/#findComment-1306261 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.