louisesanders Posted January 28, 2009 Share Posted January 28, 2009 I am trying to create a CMS – I am relatively new to PHP so please understand if this is really silly. I am trying to find the best way to store 6 different values (submitted via a form) as part of an array which I would then like to store in a database. I then need to be able to retrieve the array from the database and extract each value. I have read about the implode and explode function but I have not seen any great examples, or ones that I can understand! I have posted an excerpt of the code below. Please could someone enlighten me with an easy explanation? So in steps: 1. I want to enter values into the form 2. Submit form and store the 6 values in an array, to be stored in the database in the table called listing_images under the filed image_path. 3. Retrieve the array and extract each value. ---------------------------------------------------------------- <!--Excerpt from the form --> <form action ="process-form.php" method="post"> <p align="center"> </p> <table width="494" border="0" align="center" cellpadding="5" cellspacing="2"> <tr bgcolor="#ededed"> <td width="189">Image 1 path </td> <td width="279"><input name="image_path[]" type="text" id="imagepath[]" /></td> </tr> <tr bgcolor="#ededed"> <td>Image 2 path </td> <td><input name="image_path[]" type="text" id="image_path[]" /></td> </tr> <tr bgcolor="#ededed"> <td>Image 3 path </td> <td><input name="image_path[]" type="text" id="image_path[]" /></td> </tr> <tr bgcolor="#ededed"> <td>Image 4 path </td> <td><input name="image_path[]" type="text" id="image_path[]" /></td> </tr> <tr bgcolor="#ededed"> <td>Image 5 path </td> <td><input name="image_path[]" type="text" id="image_path[]" /></td> </tr> <tr bgcolor="#ededed"> <td>Image 6 path </td> <td><input name="image_path[]" type="text" id="image_path[]" /></td> </tr> <tr bgcolor="#ededed"> <td colspan="2"><div align="center"> <input type = "submit" value = "SUBMIT" /> </div></td> </tr> </table> </p> </form> <?php // Define variable to be posted into database, in this case it needs to be an array of the 6 values entered in via the form - Is this correct ? $image_path = $_POST['image_path']; $image_path =htmlspecialchars($image_path); //How do I insert an array into the database $sql = "INSERT INTO listing_images SET image_path = '$image_path'"; // How do I retrieve the array and extract the values. ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted January 28, 2009 Share Posted January 28, 2009 Hey! I can help ya out. explode() is pretty awesome. Here's what you could do: // this is the code to put the image paths together <?php $data = $_POST['img1'] . '\t' . $_POST['img2'] . '\t' . $_POST['img3'] . '\t'; $data .= $_POST['img4'] . '\t' . $_POST['img5'] . '\t' . $_POST['img6'] . '\t'; $query = 'INSERT INTO listing_images SET image_path="' . $data . '"'; $result = mysqli_query($con, $query); ?> // this is the code to use explode to pull them apart <?php $query = 'SELECT image_path FROM listing_images'; $result = mysqli_query($con, $query); $row = mysqli_fetch_array($result); $img_path = explode('\t', $row['image_path']); // this splits the string up at each tab // and puts each value the array so // $img_path[0] would have the first // image path, $img_path[1] would have // the second, and so on ?> oh and make sure you name the html input forms correctly to match the $_POST[] stuff example: <input name="img1" type="text" id="image_path[]" /> would be found in: $_POST['img1'] on your php page Quote Link to comment Share on other sites More sharing options...
louisesanders Posted January 28, 2009 Author Share Posted January 28, 2009 You're a huge help! Thank-you, I will try this out and see what happens! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 28, 2009 Share Posted January 28, 2009 Most of the time, storing an array in a database field is a bad idea. Why? Well, it makes searching, sorting, deleting and updating all the more difficult. Not only that, but it gives you scope to change the number of allowed images and is almost certainly going to be more efficient both in terms of time taken and space used. Almost every time you feel the need to do this, what you actually want to do is add another table. For example, you might add a another table called images with fields id,userid,imagepath. The field userid can then be used in a join to be able to select the relevant images for a particular user. If you're confused, this tutorial would be a good place to start. Quote Link to comment 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.