Jump to content

Recommended Posts

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 .  :-[

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.

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.  :shrug:

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?

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.

 

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?

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.

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  :confused:

 

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>

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.

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.