SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why wont my variable print here?!?!?!

    hi,

    I have this code, it prints a different query string depending in whether a certain row is pulled from the database. It works fine and the correct url is given depending on the situation. Problem is the $blog_id does not get in to the secon query string. ON the next page theres no number in the url just en equal sign.

    I cannot work this out. Why?!?!

    PHP Code:
    $sql = "SELECT entries.*, categories.*, uploads.* FROM entries INNER JOIN categories ON(categories.category_id = entries.category_id) LEFT JOIN uploads ON (uploads.blog_id = entries.blog_id) ORDER BY entries.date_submitted DESC LIMIT 3";
    $query = mysql_query($sql);


    while ($row = mysql_fetch_array($query)) {
      $title = $row['title'];
        $body = substr($row['body'],0,400);
        $author = $row['author_id'];
        $date = $row['date_submitted'];
        $cat = $row['category_id'];
        $cat1 = $row['category'];
        if(isset($row['upload_id'])) {
            $upload = $row['upload_id'];
        }
        else { $upload = NULL;}
        
          $blog_id  = $row['blog_id'];
        

        
    ?>






    <li><h3><?php echo $upload?><?php if(isset($upload)) { echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id;} else { echo '<a href="individualarticle.php?f=0&blog_id=' .$blog_id;} ?> "><?php echo $title?></a><?php if(isset($upload)) { echo ?><img src="pin.gif" /><?php ;}?></h3><p style="color: red; display: inline;">written by <?php echo $author?> on <?php echo $date?> Posted in<a href="categorypage.php?category_id=<?php echo $cat?>"><?php echo $cat1?></a></p><p><?php echo $body?></p></li>
    <p><a href="individualarticle.php?blog_id=<?php echo $blog_id ?>">read on...</a></p>

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Because $blog_id is outside the while loop which is handling the results from the query.

    If you want 'extra' variables outside the loop you would have to allocate the results handler and then do the loop ie: a do { } while

    PHP Code:
    $row mysql_fetch_array($query)

    do {
      
    $title $row['title'];
        
    $body substr($row['body'],0,400);
        
    $author $row['author_id'];
        
    $date $row['date_submitted'];
        
    $cat $row['category_id'];
        
    $cat1 $row['category'];
        if(isset(
    $row['upload_id'])) {
            
    $upload $row['upload_id'];
        } while (
    $row mysql_fetch_array($query));
        else { 
    $upload NULL;}
        
          
    $blog_id  $row['blog_id']; 
    Last edited by spikeZ; Feb 6, 2007 at 09:36. Reason: parse error
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI spike,

    Sorry i missed a closing bracket off of my code. It looks like this now:

    PHP Code:
    $sql = "SELECT entries.*, categories.*, uploads.* FROM entries INNER JOIN categories ON(categories.category_id = entries.category_id) LEFT JOIN uploads ON (uploads.blog_id = entries.blog_id) ORDER BY entries.date_submitted DESC LIMIT 3";
    $query = mysql_query($sql);


    while ($row = mysql_fetch_array($query)) {
      $title = $row['title'];
        $body = substr($row['body'],0,400);
        $author = $row['author_id'];
        $date = $row['date_submitted'];
        $cat = $row['category_id'];
        $cat1 = $row['category'];
        if(isset($row['upload_id'])) {
            $upload = $row['upload_id'];
        }
        else { $upload = NULL;}
        
          $blog_id  = $row['blog_id'];
        

        
    ?>






    <li><h3><?php echo $upload?><?php if(isset($upload)) { echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id;} else { echo '<a href="individualarticle.php?f=0&blog_id=' .$blog_id;} ?> "><?php echo $title?></a><?php if(isset($upload)) { echo ?><img src="pin.gif" /><?php ;}?></h3><p style="color: red; display: inline;">written by <?php echo $author?> on <?php echo $date?> Posted in<a href="categorypage.php?category_id=<?php echo $cat?>"><?php echo $cat1?></a></p><p><?php echo $body?></p></li>
    <p><a href="individualarticle.php?blog_id=<?php echo $blog_id ?>">read on...</a></p>

    <?php
    }
    ?>

    so this should make that variable in the while right? It seems to kinda work because the $blog_id does get into the query string that has f=1 in it but no the second one.

    Am i missing something here? wow ive never heard of a do while!
    Last edited by elduderino; Feb 6, 2007 at 09:58. Reason: i cant spell!

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    try:
    PHP Code:
    $sql = "SELECT entries.*, categories.*, uploads.* FROM entries INNER JOIN categories ON(categories.category_id = entries.category_id) LEFT JOIN uploads ON (uploads.blog_id = entries.blog_id) ORDER BY entries.date_submitted DESC LIMIT 3";
    $query = mysql_query($sql);


    while ($row = mysql_fetch_array($query)) {
        $blog_id  = $row['blog_id'];
        $title = $row['title'];
        $body = substr($row['body'],0,400);
        $author = $row['author_id'];
        $date = $row['date_submitted'];
        $cat = $row['category_id'];
        $cat1 = $row['category'];
        if(isset($row['upload_id'])) {
            $upload = $row['upload_id'];
        } else { 
        $upload = 0;
        }
            
    ?>

    <li>
        <h3>
            <?php echo $upload?>
                <?php if($upload == 0) { 
                     echo 
    '<a href="individualarticle.php?f=1&blog_id=' .$blog_id .'">';
                 } else { 
                     echo 
    '<a href="individualarticle.php?f=0&blog_id=' .$blog_id .'">';
                 }
                 
    ?> 
                 <?php echo $title?>
                 </a>
                 
                 <?php if(isset($upload)) { 
                      echo 
    '<img src="pin.gif" />';
                      }
                 
    ?>
        </h3>
        
        <p style="color: red; display: inline;">written by <?php echo $author?> on <?php echo $date?> Posted in<a href="categorypage.php?category_id=<?php echo $cat?>"><?php echo $cat1?></a></p>
        <p><?php echo $body?></p>
    </li>
    <p><a href="individualarticle.php?blog_id=<?php echo $blog_id?>">read on...</a></p>

    <?php ?>
    Might make no difference, if it doesn't then I will think some more!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just my guess:

    You are selecting entries.*, categories.*, uploads.*

    If you have blog_id in multiple tables (I don't know your db table layout), the script doesn't know which blog_id to print because you would be selecting blog_id multiple times. You'd have to state which blog_id you want to print out by doing

    PHP Code:
    $row['uploads.blog_id'
    I think that works. Haven't tested though.
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi guys,

    Thanks for the replies. Unfortunatley it still doesnt work. I cant see a problem with the code and kinda know its ok because it all works fine if the $upload is set.....the blog_id makes it through in to that query string.

    Creedfeed you're on the right track. I think its something to so with this bit of my query:

    PHP Code:
    uploads.blog_id entries.blog_id 
    The blog_id is only getting pulled for a record with an upload.When i echo out $blog_id in the is or else:

    PHP Code:
    if($upload == 0) {
                     echo 
    $blog_id;
                     echo 
    '<a href="individualarticle.php?f=1&blog_id=' .$blog_id .'">';
                 } else {
                     echo 
    $blog_id;
                     echo 
    '<a href="individualarticle.php?f=0&blog_id=' .$blog_id .'">';
                 } 
    It only gets printed out for entries with an upload.

    Another interesting thing is if i change the query to INNER JOIN instead of LEFT JOIN it all works. I cant quite remember why i used left join but i know there was a good reason

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved it!

    by adding this to my query: entries.blog_id here:

    PHP Code:
    SELECT entries.*, categories.*, uploads.*, entries.blog_id FROM entries INNER JOIN categories ON(categories.category_id entries.category_idLEFT JOIN uploads ON (uploads.blog_id entries.blog_idORDER BY entries.date_submitted DESC LIMIT 3 
    Woo-hoo!

    Thanks for your help guys!

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    glad it's all done and working
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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