Click on image or title go to next page...

Discussion in 'PHP' started by dannbkk, May 5, 2007.

  1. #1
    I have made a script that prints out the image and title and description on the page...

    but i would like it when they click on the image it can take them to the secondary page, is this nearlly right?

    <?php echo 'business-' . $biz_name .'-'. strtolower($row['reference']) .'.php'; ?>
     
    dannbkk, May 5, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    We need more info.

    Where does the image and description come from? A database?

    Post your code...
     
    nico_swd, May 5, 2007 IP
  3. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Yes connected to my database...

    I have the script working here now : nzsocietythai.org/nzsocietythailand.php

    but i want to have it so when i click on the image or title it will go to page with info? what code do i need to set in different page and have link on it?

    It will have say the image but larger, and the long description thats it. Nice and simple.....

    The first page is just 1 image they click from this page "nzsocietythai.org/nzsocietythailand.php" than when it takes them to the secondary page it will show the same image but much bigger, with maybe an option to have 2 more images beside it and longer description....
     
    dannbkk, May 5, 2007 IP
  4. Neoto

    Neoto Active Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4
    If I understood your message correctly, you just need to put the img-tag and the title inside a link like this:

    
    <?php
     $q = mysql_query("SELECT * FROM news ORDER BY `date` DESC");
    
     if (mysql_num_rows($q) > 0) {
     	while ($rec = mysql_fetch_assoc($q)) {
     		echo "<div class=\"news_entry\">\n";
     		echo "[COLOR="Red"]<a href=\"secondarypage.php\">[/COLOR]<h1>$rec[title]</h1>[COLOR="Red"]</a>[/COLOR]\n";
     		echo !empty($rec['image']) ? "[COLOR="Red"]<a href=\"secondarypage.php\">[/COLOR]<img src=\"images/$rec[image]\" />[COLOR="Red"]</a>[/COLOR]\n" : '';
     		echo "<p>" . nl2br($rec['content']) . "</p>\n";
     		echo "<p>Posted " . date('F j, Y', strtotime($rec['date'])) . "</p>\n";
     		echo "</div>\n";
     	}
     }
    ?>
    
    Code (markup):
    Now the picture and title will link to another page, in this case to secondarypage.php.
     
    Neoto, May 5, 2007 IP
  5. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Yeah thats great, now if i want to display code on secodarypage.php? to have the same image but "larger image" and with the "full description" of text how do i set the php code differently.
     
    dannbkk, May 5, 2007 IP
  6. Neoto

    Neoto Active Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #6
    Well, that depends on your environment, where do you keep the images and where do you have the full description.

    But the basic idea is that you call the other page with some kind of unique id and create the page based on that.

    For example, lets forget secondarypage.php completely and change the code you gave earlier to:

    
    <?php
     $q = mysql_query("SELECT * FROM news ORDER BY `date` DESC");
    
     if (mysql_num_rows($q) > 0) {
     	while ($rec = mysql_fetch_assoc($q)) {
     		echo "<div class=\"news_entry\">\n";
     		echo "[COLOR="Red"]<a href=\"news.php?id=$rec[id]\">[/COLOR]<h1>$rec[title]</h1>[COLOR="Red"]</a>[/COLOR]\n";
     		echo !empty($rec['image']) ? "[COLOR="Red"]<a href=\"news.php?id=$rec[id]\">[/COLOR]<img src=\"images/$rec[image]\" />[COLOR="Red"]</a>[/COLOR]\n" : '';
     		echo "<p>" . nl2br($rec['content']) . "</p>\n";
     		echo "<p>Posted " . date('F j, Y', strtotime($rec['date'])) . "</p>\n";
     		echo "</div>\n";
     	}
     }
    ?>
    
    Code (markup):

    Now you can create news.php and do something like this in it:

    
    <?php
     // Get the unique id
     $id = floatval($_GET['id']);
     // Fetch the corresponding news item from the database
     $q = mysql_query("SELECT * FROM news WHERE id = '{$id}'");
     $rec = mysql_fetch_assoc($q);
    
     // Print the title
     echo "<h1>$rec[title]</h1>\n";
     // Print the image
     echo !empty($rec['image']) ? "<img src=\"images/$rec[image]\" />\n" : '';
     // Print the full description
     echo "<p>" . nl2br($rec['full_description']) . "</p>\n";
    ?>
    
    Code (markup):
    I haven't tested the code, so I'm not 100% it will work, but you should still get the basic idea from it.
     
    Neoto, May 5, 2007 IP
  7. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Thats bloody brilliant!!! Yeah im still playing with it though I did get an error? ANy ideas why?
     
    dannbkk, May 5, 2007 IP
  8. Neoto

    Neoto Active Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #8
    What error messages are you getting?

    I made a few obvious typos in the code, but they should be fixed now. Unfortunately I don't have an opportunity to test the code at the moment, so there may be some more errors.
     
    Neoto, May 5, 2007 IP
  9. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #9
    If you go this URL - nzsocietythai.org/nzsocietythailand.php and click on the image or title it gives me an error like so;

    I didnt change anything here, only changed the word "news" to "events"

    my table row in mysql for description is named "content" does that need to be change accordingly?
     
    dannbkk, May 5, 2007 IP
  10. Neoto

    Neoto Active Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #10
    Ok, the code I gave you was just an example, not the full working thing. :)

    To make it work, you just need to add the mysql-connection opening before the mysql_query line, something like this:

    
    $link = mysql_connect("localhost", "mysql_user", "mysql_password");
    mysql_select_db("db", $link);
    
    PHP:
    I'm assuming the connection is working in nzsocietythailand.php so you can copy these lines from there.

    I put the $rec['full_description'] there just as an example on how to print full description to the page. So you have to change it to content, if that's the column you want to use.
     
    Neoto, May 5, 2007 IP
  11. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #11
    Ok I added that line but i still got the same error, im getting the feeling i need to create an "id" row in my database?

    in my id line i have this

    CREATE TABLE news (
    id integer unsigned auto_increment primary key,

    do i need it to something like this?
    `id` tinyint(8) NOT NULL auto_increment,
     
    dannbkk, May 5, 2007 IP
  12. Neoto

    Neoto Active Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #12
    If you can create the database tables without any error messages, you shouldn't have to change the id line.

    Are you getting the exatcly same error message as earlier (Access denied for user...)?

    If so does the mysql-connection work on the other page? Are you opening the mysql connection the same way as on the other page?


    It's kind of hard to debug the problem without seeing the whole code. If you paste the code here or send it as a personal message to me, I could take a closer look.
     
    Neoto, May 6, 2007 IP