views:

32

answers:

2

Hello

I am trying to auto generate pages.

What I am trying to do is make a upload form for an image upload that's displayed in a gallery.

I have this but I then want each image to have a page hyper-link auto made for each image where the image can be seen bigger with buying information that's also been uploaded to a MySQL table not 100% with codeigniter I am still luring please look at the site I am trying to build at http://www.fresherdesign.co.uk/PIFF/index.php/main/gallery

This is a direct link to the gallery that I would link to make the page's from, currently they just open a direct link to the image on its own

Any help would be awesome thanks to everyone in advance

Alan Morton

A: 

Precondition

You'll need to have the directory name in which things are stored, relative to where your php page is located. Obviously, you have this already to create the page. I will assume it is stored in the $dir variable.

Code

if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        echo "<a href='http://www.mydomain.com/".$dir."/".$file."'&gt;";
        echo "<img src='http://www.mydomain.com/".$dir."/".$file."' />";
        echo "</a>";
    }
}

Output

This will provide you a list of images that link to the image file itself.

Possible Modifications

1) You may want to only show some of the files in the directory, or a certain amount: Use the if block added here to do that:

if ($handle = opendir($dir)) {
    //set $strThatSpecifiesImages to whatever you want; 
    //for the example, I'm saying that we only want to show files with "gal_image" in the filename
    $strThatSpecifiesImages = "gal_image"; 
    $maxFilesToShow = 10; //set this to whatever you want; example max is 10
    $count = 0;
    while (($count < $maxFilesToShow) && (false !== ($file = readdir($handle))) {
        if (strpos($file, $strThatSpecifiesImages) > -1) {
           echo "<a href='http://www.mydomain.com/".$dir."/".$file."'&gt;";
           echo "<img src='http://www.mydomain.com/".$dir."/".$file."' />";
           echo "</a>";
           $count++;
        }
    }
}

2) You might also want to change how things are displayed, so that you don't just have the full image shown here every time. Do this by modifying things in the echo blocks that are outputting HTML. You can have the browser resize the images, or just change it completely so that the links use text instead, or something else.

JGB146
A: 

If you want the actual images to be uploaded (and not stored in the database - note that db storage is slightly more difficult to accomplish than your standard upload), you can create a field in the database for the image's location; that is how you are going to correspond your image data with your page content.

Now, if you want a hyperlink to automatically be made, I suggest querying the database of all "Active" entries (again, could be another field unless you simply delete old entries). Each entry should have a unique ID associated with it. This way, when you give the list, simply include a tag similar to

while($row = mysql_fetch_array($query_result)){
    // Change this to whatever formatting you need
    print '<a href="your_content_page.php?id=<?php print $row['ID']; ?>"><!-- Whatever content for link --></a>';
}

Now that's just your loop to get the results. The actual page now should query the database based on the ID given. The query should grab the page content from the database. Something like this would work

<?php
if(!isset($_GET['id'])){
  die("Please use a valid link!");
}

$q = mysql_query("SELECT * FROM YOUR_DB_TABLE WHERE ID='".mysql_real_escape_string($_GET['id'])."' LIMIT 1;");

if(!$q || mysql_num_rows($q) < 1){
  die("A MySQL Error Occurred: ".mysql_error());
}

while($row = mysql_fetch_array($q)){
    // Again, adjust this accordingly
    print "Data column 1: ".$row['DataRow1']."<br />".
    "Data Column 2: ".$row['DataRow2']."<br />".
    "<img src='".$row['ImageLocation']."' />";
}

?>

Now, I haven't tested this exact code, however all of it should work as is. But you will need to adjust it appropriately to fit your requests; but this is one way to accomplish what you're looking for.

RageD
I am a little confused do I need to store the link to the image some how on the database? I can show the full image from the director using codeigniter do I still need to store the image or the link to the image on the database? I dont really feel the need to store the image in the database. all I am after is to auto a link to each image, would you 1st code do this for me? quite new to codeigniter so big thanks for your help.
It depends on what you are trying to accomplish. You definitely do _NOT_ need to store the image into the database. It's probably best not to, in fact, for your situation. If all your images are stored in the same directory, you can store that information with the content in the database. Simply store the file name. If you are always storing the exact file name, it will always be $_FILES[0]['name']. If you're randomizing it, store that in the database and then output the link (similar to the first example) as yoursite.co.uk/your_image_dir/".$row['file_name']or whatever the row is called.
RageD