Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool PHP loops problem.

    Hi, I'm trying to update a current pictures site from html into php with a mysql backend, im pretty new to php and mysql and have run into a problem I cant seem to remedy??, any help would be great!.

    I have set up the database, and in the database I have 2 tables so far, one is for the pictures categories which has the names of the categories, and the urls, and the other is for the pictures which has the title of the pictures, and filenames and description, so I linked them both with a associated primary/foreign key relationship.

    Now my problem is with a section on my site I would like the latest pictures posted to be shown, so I used a href so you can click on anyone and it will take you to that pictures category, the only problem is I get the latest pictures showing up fine, but when I put the mouse on them the url is the same all the way down, rather than change for each individual picture category, it stays the same link?, ive tried as much as I know so far, but still the same problems?, what can I do?.

    PHP Code:
    mysql_select_db($database, $connDB) OR DIE ('Unable To Select That Database' . mysql_error() );

    $query_sPictures = "SELECT site_pictures.sp_filename_TN, site_pictures.sp_ALT FROM site_pictures ORDER BY site_pictures.sp_timestamp ASC";
     
    $sPictures = @mysql_query($query_sPictures, $connDB) or die(mysql_error());
    $row_sPictures = mysql_fetch_assoc($sPictures);
    $totalRows_sPictures = mysql_num_rows($sPictures);  <-- This part works fine
    ?> 

    <?php
    $query_sCategories 
    "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id";
     
    $sCategories = @mysql_query($query_sCategories$connDB) or die(mysql_error());
    $row_sCategories mysql_fetch_assoc($sCategories);
    $totalRows_sCategories mysql_num_rows($sCategories);
    ?>

    <?php do { ?><a href="<?php echo $row_sCategories['sc_url'];?>"> <-- This is where the problem is??.

    <img src="pictures/<?php echo $row_sPictures['sp_filename_TN']; ?>" alt="<?php echo $row_sPictures['sp_ALT'];?>"></img>[/url]

    <?php } while ($row_sPictures mysql_fetch_assoc($sPictures));


    mysql_close();?>
    Any help would be great, im pulling my hair out with this!!!!!!.
    Other forums have not been too helpfull!!.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    just a note:
    if your code was cleaner more people would look at it.

    PHP Code:
    <%
    mysql_select_db($database$connDB) OR DIE ('Unable To Select That Database' mysql_error() ); 

    $query_sPictures "SELECT site_pictures.sp_filename_TN, site_pictures.sp_ALT FROM site_pictures ORDER BY site_pictures.sp_timestamp ASC"
    $sPictures = @mysql_query($query_sPictures$connDB) or die(mysql_error()); 
    $row_sPictures mysql_fetch_assoc($sPictures); 
    $totalRows_sPictures mysql_num_rows($sPictures);

    $query_sCategories "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id"
    $sCategories = @mysql_query($query_sCategories$connDB) or die(mysql_error()); 
    $row_sCategories mysql_fetch_assoc($sCategories); 
    $totalRows_sCategories mysql_num_rows($sCategories); 

    while (
    $row_sPictures mysql_fetch_array($sPictures)) {
        echo 
    "<a href='" $row_sCategories['sc_url'] . "'><img src='pictures/" $row_sPictures['sp_filename_TN'] . "' alt='" $row_sPictures['sp_ALT'] . "' /></a>";
    }

    mysql_close();
    ?> 
    try that.

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not do something like this in lieu of the code you provided.

    PHP Code:
    <?php
    $qry 
    "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
    $rslt = @mysql_query($qry$connDB) or die(mysql_error());
    $total_rows mysql_num_rows($rslt);
    while (
    $row mysql_fetch_assoc($rslt)){
      echo 
    "<a href=\"$row['sc_url']\"><img src=\"pictures/$row['sp_filename_TN']\" alt=\"$row['sp_ALT']\"></a>";
    }
    mysql_close($connDB);
    ?>

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    haha, i didn't even wanna think that much about it,
    sort of hurts just to look at ;-P

    but, practice makes perfect

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by angst View Post
    haha, i didn't even wanna think that much about it,
    sort of hurts just to look at ;-P

    but, practice makes perfect
    Couldn't agree with you more, Angst. My intial response was directed to eDawg and didn't mean to seem like I was commenting on your suggestion.

    eDawg,
    As Angst suggested please try to clean up your code before just copying and pasting. This will help others when then attempt to look through your code to help.

    Good luck!

  • #6
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by martialtiger View Post
    Why not do something like this in lieu of the code you provided.

    PHP Code:
    <?php
    $qry 
    "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
    $rslt = @mysql_query($qry$connDB) or die(mysql_error());
    $total_rows mysql_num_rows($rslt);
    while (
    $row mysql_fetch_assoc($rslt)){
      echo 
    "<a href=\"$row['sc_url']\"><img src=\"pictures/$row['sp_filename_TN']\" alt=\"$row['sp_ALT']\"></a>";
    }
    mysql_close($connDB);
    ?>
    Hi, and thanks for your help!!!, I will clean up code in the future, because ill probably be in here alot.

    I just tried the code you gave me, but get this error,

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
    expecting T_STRING or T_VARIABLE or T_NUM_STRING in file/latest_pictures.php on line 6


  • #7
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this
    PHP Code:
    <?php
    $qry 
    "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
    $rslt = @mysql_query($qry$connDB) or die(mysql_error());
    $total_rows mysql_num_rows($rslt);
    while (
    $row mysql_fetch_assoc($rslt)){
      echo 
    "<a href=\"" $row['sc_url'] . "\"><img src=\"pictures/" $row['sp_filename_TN'] . "\" alt=\"" $row['sp_ALT'] . "\"></a>";
    }
    mysql_close($connDB);
    ?>
    If that doesn't fix it you may need to insert the codes 1-10 of lastest_pictures.php just so we can take a look further into it.

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    make sure to clean up any white spaces ( blank spaces ).
    the code looks ok otherwise.

  • #9
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All I can say is thank for both of your help!!!, I actually modified it some more into this -->

    PHP Code:
    <?php
    $qry 
    "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
    $rslt = @mysql_query($qry$connDB) or die(mysql_error());
    $row_result mysql_fetch_assoc($rslt);
    $total_rows mysql_num_rows($rslt);?>
    <?php
    do { ?><a href="<?php echo $row_result['sc_url']; ?>"><img src="pictures/<?php echo $row_result['sp_filename_TN'];?>" alt="<?php echo $row_result['sp_ALT']; ?>"></a>
    <?php } while ($row_result mysql_fetch_assoc($rslt)) ;

    mysql_close($connDB);

    ?>
    And it now works!! , I could not of done it without your help!!, and I learned along the way!!.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •