richardsanchez@hotmail Posted October 24, 2009 Share Posted October 24, 2009 Hello, I have a MySQL db were I store articles in. I have a form to fill these articles, and in that form I can select images who are stored into a directory. Selecting the images works good but the problem is when I want to edit the stored article then the image is not saved and I again have to select it from the selectdropdown box. Basicly I want to match the stored value with the current value in my loop. Is there a way that the earlier saved image into an article keeps up with the rest from the article but keep it dynamic so if I want to, I still van change the image easy. My code: <select name="content_img" id="content_img"> <option value=""> <?php $dirPath = dir('media/images/content'); $imgArray = array(); while (($file = $dirPath->read()) !== false) { if ((substr($file, -3)=="gif") || (substr($file, -3)=="jpg") || (substr($file, -3)=="png")) { $imgArray[ ] = trim($file); } } $dirPath->close(); sort($imgArray); $c = count($imgArray); for($i=0; $i<$c; $i++) { $selected = (isset($image) && $image == $imgArray[$i]) ? "selected" : ""; echo "<option value='{$imgArray[$i]}' $selected>{$imgArray[$i]}</option>\n"; } ?></option> </select> HELP...please . Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/ Share on other sites More sharing options...
tryingtolearn Posted October 24, 2009 Share Posted October 24, 2009 When writing the article and you select the image Can(are) you store the image name in the DB? Then when you go to edit the article it will use that image name but if you want to change it you can just pick a different one and update the name in the DB. Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943440 Share on other sites More sharing options...
richardsanchez@hotmail Posted October 24, 2009 Author Share Posted October 24, 2009 When writing the article and you select the image Can(are) you store the image name in the DB? Then when you go to edit the article it will use that image name but if you want to change it you can just pick a different one and update the name in the DB. Well, the image name is stored in the article that is stored in the DB. But the image name does not show when I want to edit that article again through my CMS. When I open my MSQL db directly than the image is stored OK. When Ik open the aticle op a webpage than the selected image is shown. Only in edit mode the image name is empty. Can't figger out how to fix this. Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943461 Share on other sites More sharing options...
richardsanchez@hotmail Posted October 24, 2009 Author Share Posted October 24, 2009 On another page where I can read the articles with other content I use this code to show the image: echo "<img src=\"media/images/content/".$bericht["content_img"]."\" id=\"articleimg\" />"; This works fine, but how can I use the same image to be selected in the edit mode? Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943534 Share on other sites More sharing options...
DavidAM Posted October 24, 2009 Share Posted October 24, 2009 It looks like you have the logic correct to show the current image in the select list for($i=0; $i<$c; $i++) { $selected = (isset($image) && $image == $imgArray[$i]) ? "selected" : ""; echo "<option value='{$imgArray[$i]}' $selected>{$imgArray[$i]}</option>\n"; } (by the way, use code tags to make your code easier to read) To make this work, you need to assign the image name to the $image variable before the loop somewhere. I don't see you doing that in the portion of code you posted. Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943538 Share on other sites More sharing options...
richardsanchez@hotmail Posted October 24, 2009 Author Share Posted October 24, 2009 To make this work, you need to assign the image name to the $image variable before the loop somewhere. I don't see you doing that in the portion of code you posted. Ok, you're right. Can you tell me how to do that? Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943626 Share on other sites More sharing options...
DavidAM Posted October 25, 2009 Share Posted October 25, 2009 I don't know where you want to get the image name from. The logic in your code assumes it is stored in a variable called $image. So, whereever you have the image name stored (database or whatever) you need to get it and assign it to that variable. Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943765 Share on other sites More sharing options...
richardsanchez@hotmail Posted October 25, 2009 Author Share Posted October 25, 2009 I don't know where you want to get the image name from. The logic in your code assumes it is stored in a variable called $image. So, whereever you have the image name stored (database or whatever) you need to get it and assign it to that variable. It's stored into a database field called: content_img I've changed $image to $content_img but this still does not work Isn't this the way to assign it? My full code now is: <select name="content_img" id="content_img"> <option value=""> <?php $dirPath = dir('media/images/content'); $imgArray = array(); while (($file = $dirPath->read()) !== false) { if ((substr($file, -3)=="gif") || (substr($file, -3)=="jpg") || (substr($file, -3)=="png")) { $imgArray[ ] = trim($file); } } $dirPath->close(); sort($imgArray); $c = count($imgArray); for($i=0; $i<$c; $i++) { $selected = (isset($content_img) && $content_img == $imgArray[$i]) ? "selected" : ""; echo "<option value='{$imgArray[$i]}' $selected>{$imgArray[$i]}</option>\n"; } ?> </option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943909 Share on other sites More sharing options...
richardsanchez@hotmail Posted October 25, 2009 Author Share Posted October 25, 2009 I've put another echo field in the article where I show the current image name without a selection box: <?php echo htmlspecialchars("")." " ?> <textarea cols="40" rows="1" name="content_img" maxlength="255"><?php echo str_replace('"', '"', trim($row["content_img"])) ?></textarea> This code works perfect. It shows the image name. Now I want to include this image preselected in an option value as mentioned before. Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-943970 Share on other sites More sharing options...
DavidAM Posted October 25, 2009 Share Posted October 25, 2009 Somewhere before the for loop, you need to assign that image name to the variable: $content_image = trim($row["content_img"]); Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-944333 Share on other sites More sharing options...
richardsanchez@hotmail Posted October 26, 2009 Author Share Posted October 26, 2009 Somewhere before the for loop, you need to assign that image name to the variable: $content_image = trim($row["content_img"]); THX it works ! Quote Link to comment https://forums.phpfreaks.com/topic/178833-solved-match-stored-value-with-the-current-value-in-a-loop/#findComment-944558 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.