Hello all,

Cek this page: image gallery

That's static image gallery - and I am trying to create backend for it.

If I upload new pictures to edit the existing pictures, they will have random name, then how does the computer now where to place each random name ? (since normally you should now the exact name to code where to locate the picture)

Recommended Answers

All 10 Replies

Hmm what you could do is make your system remember where it puts which images. For example if it saves an image under the name "randomname123.jpg" and that image belongs to page 1, you could put a record in your database saying that "page 1" is connected to "randomname123.jpg", if you get what I mean :)?

hmm.. how to code it to make the system remember?

Well I mean something like this:

When the user uploads in image through your back-end, you give it a random name and place it into a specific folder. At that moment, you know the file's name. You can then insert a record into a database table you will have to create for this specific purpose, that will help you remember which entity this image is connected to. Let's say the user is uploading an image of a product in your webshop. You would then create a table like: product_images, which would hold records like: product_id = 1, image_name = $random_image_name, connecting the images to the products. Then, when a product is loaded in the front end, you could execute a query like: select * from product_images where product_id = [product_id]. That would give you some file names, which you can then use in your <img> tags :). Is this clear enough? Hope I could be of help!

Well, here I am trying to code the front end:

<?php

include('includes/koneksi.php');

$result1 = mysql_query("SELECT * FROM title WHERE gallery_id = 1");
$result2 = mysql_query("SELECT * FROM title WHERE gallery_id = 2");
$result3 = mysql_query("SELECT * FROM title WHERE gallery_id = 3");
$result4 = mysql_query("SELECT * FROM title WHERE gallery_id = 4");

$data1 = mysql_fetch_array($result1)
$data2 = mysql_fetch_array($result2)
$data3 = mysql_fetch_array($result3)
$data4 = mysql_fetch_array($result4)


<div class = "position1"><img src="'photos/'.$data1['title']"></div>

<div class = "position2"><img src="'photos/'.$data2['title']"></div>

<div class = "position3"><img src="'photos/'.$data3['title']"></div>

<div class = "position4"><img src="'photos/'.$data4['title']"></div>

?>

also let's say MAX 4 pictures.

Why don't you just run one query for all of your galleries, then use only one array, and then loop through that?

Then you would have something like this for a databast structure:

Galleries (gallery_id,gallery_name,create_date)
Images (image_id,gallery_id,image_name,file_path,upload_date)

It would be a little more simplified this way.

I also use one gallery.

gallery(image_id, image, title)

image = filename

The target path is consistent, always photos/ that's why I didn't include path.

This is my revise front end code:

<?php

include('includes/koneksi.php');

$result = mysql_query("SELECT image FROM gallery") or die(mysql_error());

$data = mysql_fetch_array($result)


echo "<div class = "ex1"><img src="'photos/'.$data['image']'"></div>";
echo "<div class = "ex2"><img src="'photos/'.$data['image']'"></div>";
echo "<div class = "ex3"><img src="'photos/'.$data['image']'"></div>";
echo "<div class = "ex4"><img src="'photos/'.$data['image']'"></div>";

?>

Parse error: syntax error, unexpected T_ECHO in C:\xampp\htdocs\RustoleumCustomCMS2\aerosol-produk.php on line 142

line 142: echo "<div class = "ex1"><img src="'photos/'.$data['image']'"></div>";

I also have errors for part of the backend:

    Show current image list <br><br>

<?php

$result = mysql_query("SELECT * FROM title") or die(mysql_error());
?>

<table border="1" cellpadding="2">
    <tr>
        <th>Number</th>
        <th>Judul</th>
        <th>Image</th>
        <th>Action</th>
    </tr>

<?php    
while($data = mysql_fetch_array($result)){

    echo "<tr>";
    echo '<td>'.$data['id'].'</td>';
    echo '<td>'.$data['judul'].'</td>';
    echo '<td><img src="'../photos/'.'$data['image']'"></td>'; 
    echo "<td>Edit | Hapus</td>";
    echo "</tr>" 
}

?>  

Parse error: syntax error, unexpected '.' in C:\xampp\htdocs\RustoleumCustomCMS2\administrator\input_image.php on line 195

line 195: echo '<td><img src="'../photos/'.'$data['image']'"></td>';

Why is it?

Error is from mixup of double and single quotes. Look at this example:

Incorrect:

echo "<div class = "ex1"><img src="'photos/'.$data['image']'"></div>";

Correct:

echo "<div class = 'ex1'><img src='photos/".$data['image'] ."'></div>";

Same issue with the other error.

This time, for backend. I am trying to delete unnecessary row:

    //Hapus image

    if (!empty($_REQUEST['mode'])&& !empty($_REQUEST['id'])){
        $id = $_REQUEST['id'];
        $result = mysql_query("DELETE * FROM gallery WHERE id = $id");

        $confirmation = $result ? "Data telah terhapus." : "Gagal menghapus data."; 
    }


    <?php

$result = mysql_query("SELECT * FROM gallery") or die(mysql_error());
?>

<table border="1" cellpadding="2">
    <tr>
        <th>Number</th>
        <th>Judul</th>
        <th>Image</th>
        <th>Action</th>
    </tr>

<?php    
while($data = mysql_fetch_array($result)){

    echo "<tr>";
    echo "<td>".$data['id']."</td>";
    echo "<td>".$data['judul']."</td>";
    echo "<td><img src='../photos/".$data['image']."'></td>";  
    echo "<td>Edit |<a href='input_image.php?mode=delete&id=".$data['id']."'>Hapus</a></td>";
    echo "</tr>";    
}

?>  

When I press hapus (or delete) why nothing happen ?

The url: http://localhost/RustoleumCustomCMS2/administrator/input_image.php?mode=delete&id=0 (have mode and id)

Nevermind, I find the error for that one, sql syntax.

Now, there is another one:

//Load berita
if (!empty($_REQUEST['id'])){
    $result = mysql_query("SELECT * FROM gallery WHERE id =".$_REQUEST['id']) or die(mysql_error());
    $data = mysql_fetch_array($result);
    $id = $data['id'];
    $judul = $data['judul'];
    $image = $image['image'];

}

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\RustoleumCustomCMS2\administrator\input_image.php on line 137

line 137: $image = $image['image'];

Nice work! Make sure to mark this solved if it is. Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.