SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can't get $id to the database...

    Hi,

    I'm working on getting an article to break up into several pages. The problem is that I'm kinda totally lost.

    This is what I have done:
    PHP Code:
    <?
    ob_start
    ("gz_handler");
    function 
    get_article($articles_id,$page){
        
        
    $info get_article_info($articles_id);

        
    ob_start();
        
    highlight_string(stripslashes($info['articles_text']));
        
    $buffer ob_get_contents();
        
    ob_end_clean();
        
        
    $story $buffer;
        
    function 
    get_article_info($articles_id){
    global 
    $page$articles_id;
        
    $sql "SELECT ssarticles.*, ncat_name, user_id, user_nick 
            FROM ssarticles, ssusers, ssnewscats
                WHERE news_cat = ncat_id
            AND news_user = user_id
            AND ssarticles.articles_id = 
    $articles_id";

        
    $r mysql_query($sql);

        if(@
    mysql_num_rows($r)){
            return 
    mysql_fetch_array($r);
        }else{
        return 
    0;
        }
    }

        
    // STUFF FOR ARTICLE MANAGEMENT
        // If no page specified, default to the first page ($page = 0)
        
    if (!strlen($page)){
            
    $page 0;
        }

        
    // Split the text into an array of pages
        
    $textarray split("\[pagebreak]",$story);

        
    // Select the page we want
        
    $story $textarray[$page];

        if (
    $page != 0) {
            
    $prevpage $page 1;
            
    $pages[] = '<a href="http://www.objeqt.com/maniacs/articles.php?id='.$articles_id.'&'.$prevpage.'">'.
                 
    'Previous Page</a>';
        }

        if(
    sizeof($textarray)){
            for(
    $i $i sizeof($textarray) ; ++$i){
                if(
    $page == $i){
                    
    $pages[] = ($i 1);
                }else{
                    
    $pages[] = '<a href="http://www.objeqt.com/maniacs/articles.php?id='.$articles_id.'&'.$i.'">'.($i 1).'</a>';
                }
            }
        }

        if (
    $page count($textarray) - 1) {
            
    $nextpage $page 1;
            
    $pages[] = '<a href="http://www.objeqt.com/maniacs/articles.php?id='.$articles_id.'&'.$nextpage.'">'.
                 
    'Next Page</a>';
        }
            
    $story .= '<center>[ '.implode(' ][ ',$pages).' ]</center>';

        return 
    nl2br($story);
    }
    function 
    print_story($story
    {
        echo 
    $story;
    }

    if(!
    strlen($articles_id)){
        echo 
    'NO ID!!!';
    }else{
        
    $sql "SELECT * FROM ssarticles WHERE articles_id='$articles_id'";
        
    $r mysql_query($sql);
        if(@
    mysql_num_rows($r)){
        
    $row mysql_fetch_array($r);
        while(list(
    $key,$val) = each($row)){
            $
    $key $val;
            }

        
    print_story($story);
        }else{
        echo 
    'No Record with ID of '.$articles_id.'!!!<br>';
        echo 
    'SQL: '.$sql;
        }
    }
    ?>
    The problem that I'm experiencing is that I'm not getting the $articles_id into the select.

    Any suggestions to what I should do? Am I even doing this right?

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    Dallas/Ft. Worth
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re:

    Question: is your $article_id a string or an integer? If it is an integer, then your query will not succeed since you're trying to use it as a string (with quotes (')).

    Make sure you print out your query to see whether id is there or not. Perhaps, on your page, if there is more code, another place uses the same variable name and resets the $article_id.

    Finally, a design issue but maybe you're already doing this. $article_id is probable a key in your table, and as being a key, you should make it an integer so that your queries are fast - database engine makes optimizations and can do faster queries if the key as integer rather than an alphanumeric string.

    I hope this helps! Good luck!

  3. #3
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, articles_id is an integer!
    I removed quotes on all the places where I had them, they now look like this:

    PHP Code:
            $pages[] = '<a href="http://www.objeqt.com/maniacs/articles.php?id=.$articles_id.&'.$prevpage.'">'
                 
    'Previous Page</a>'
    But I still have the same problem. Could it be anything else?

    I tried something called error-trapping and this is what I got in return:

    SELECT ssarticles.*, ncat_name, user_id, user_nick FROM ssarticles, ssusers, ssnewscats WHERE news_cat = ncat_id AND news_user = user_id AND ssarticles.articles_id = "

    That's how I figured that $articles_id isn't being set.
    Last edited by object; Oct 27, 2001 at 02:26.

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    Dallas/Ft. Worth
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re:

    Have you checked that you have the value for $article_id prior to creating the sql statement? Just a simple echo or printf can tell you that. If it is "alive" at that point, then try creating your string without string concatination operator (the . operator). Instead write something like this:

    $my_string = "http://myurl.com/test.php?id=$article_id&other_var=$otherVar";

    instead of

    $my_string = "http://myurl.com/test.php?id=".$article_id."&"."other_var=$otherVar";

    Let me know if it works but test that $article_id exists and is not empty if this doesn't work. Check "empty()" function.

    Good luck!

  5. #5
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for helping me.

    I tried without concatination operator and it was still the same problem then I echo() the $articles_id and it was empty so you're right about that I'm not "setting" it anywhere.

    I looked at the functions on top and they don't seem ok but I can't figure out what the problem is.

    Can anybody help me to debug this?
    PHP Code:
    <?
    ob_start
    ("gz_handler");
    function 
    get_article($articles_id,$page){
        
        
    $info get_article_info($articles_id);

        
    highlight_string(stripslashes($info['articles_text']));
        
    function 
    get_article_info($articles_id){
    global 
    $page$articles_id;
        
    $sql "SELECT ssarticles.*, ncat_name, user_id, user_nick 
            FROM ssarticles, ssusers, ssnewscats
                WHERE news_cat = ncat_id
            AND news_user = user_id
            AND ssarticles.articles_id = 
    $articles_id";

        
    $r mysql_query($sql);

        if(@
    mysql_num_rows($r)){
            return 
    mysql_fetch_array($r);
        }else{
        return 
    0;
        }
    }

        
    // STUFF FOR ARTICLE MANAGEMENT
        // If no page specified, default to the first page ($page = 0)
        
    if (!strlen($page)){
            
    $page 0;
        }

        
    // Split the text into an array of pages
        
    $textarray split("\[pagebreak]",$story);

        
    // Select the page we want
        
    $story $textarray[$page];

        if (
    $page != 0) {
            
    $prevpage $page 1;
            
    $pages[] = '<a href="http://www.objeqt.com/maniacs/articles.php?id=$articles_id&$prevpage">'.
                 
    'Previous Page</a>';
        }

        if(
    sizeof($textarray)){
            for(
    $i $i sizeof($textarray) ; ++$i){
                if(
    $page == $i){
                    
    $pages[] = ($i 1);
                }else{
                    
    $pages[] = '<a href="http://www.objeqt.com/maniacs/articles.php?id=$articles_id&$i">'.($i 1).'</a>';
                }
            }
        }

        if (
    $page count($textarray) - 1) {
            
    $nextpage $page 1;
            
    $pages[] = '<a href="http://www.objeqt.com/maniacs/articles.php?id=$articles_id&$nextpage">'.
                 
    'Next Page</a>';
        }
            
    $story .= '<center>[ '.implode(' ][ ',$pages).' ]</center>';

        return 
    nl2br($story);
    }
    function 
    print_story($story
    {
        echo 
    $story;
    }

    if(empty(
    $articles_id)){
        echo 
    'NO ID!!!';
    }else{
        
    $sql "SELECT * FROM ssarticles WHERE articles_id=$articles_id";
        
    $r mysql_query($sql);
        if(@
    mysql_num_rows($r)){
        
    $row mysql_fetch_array($r);
        while(list(
    $key,$val) = each($row)){
            $
    $key $val;
            }

        
    print_story($story);
        }else{
        echo 
    'No Record with ID of .$articles_id.!!!<br>';
        echo 
    'SQL: '.$sql;
        }
    }
    ?>
    Last edited by object; Oct 27, 2001 at 10:50.

  6. #6
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just can't figure this out.
    Anyone can help me?

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    Dallas/Ft. Worth
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re:

    I am not sure how much experience you have but you need to learn how to debug your code. So, in general, use echo or printf functions to see the values of variables, and you'll find where you're losing your values.

    You also said that you are not setting the variable $articles_id anywhere...well, if you're not setting it anywhere, you cannot expect to have the value in that variable by magic. You have set it somewhere.

    You are declaring functions...and when you add parameters to a functions, the parameter should have a different name than the variable you're passing in. You should never write "function getId($id)" if you already have a variable called $id that you will pass to that function by writing getId($id).

    So, don't stress, write a lot of echo's in your code and find out where the jam is.

    Good luck!

  8. #8
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for helping me.

    Of course that I don't expect for $articles_id to appear by magic.

    What I meant was that it isn't being set although I've tried setting it.

    I've tried doing it here:
    PHP Code:
    function get_article_info($articles_id){
    global 
    $page$articles_id;
        
    $sql "SELECT ssarticles.*, ncat_name, user_id, user_nick 
            FROM ssarticles, ssusers, ssnewscats
                WHERE news_cat = ncat_id
            AND news_user = user_id
            AND ssarticles.articles_id = 
    $articles_id"
    The problem is that I don't knowhow to set it because view_articles.php should read the ?id=$articles_id and based on that show the article. That's the general idea, maybe I'm doing this completely wrong???

    I don't have much experience as you might have noticed.

    I'll keep working on it.

  9. #9
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    Dallas/Ft. Worth
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re:

    Well, I guess, somebody must have helped you a lot because the code is not very straight forward. What you see there are bunch of functions which you need to use to print out your story.
    So, in the part:
    -----------------------
    if(!strlen($articles_id)){
    echo 'NO ID!!!';
    }else{
    $sql = "SELECT * FROM ssarticles WHERE articles_id='$articles_id'";

    ....

    echo 'No Record with ID of '.$articles_id.'!!!<br>';
    echo 'SQL: '.$sql;
    }
    }
    ------------------------
    which is the main part of your code, you need to use your functions to get your article to display. So, this main part should look something like this (I'll write pseudo code to give you the big picture and you worry about the syntax):
    -----------------------------
    1. Check if $article_id and $page exist and if the don't send error using header();
    2. if the exist, call getArticle($article_id, $page) to get and print the story.

    I don't see the need for yet another sql statement.

    Also, you should execute this file by typing something like:

    http://yourserver.com/view_articles...._id=10&page=0.

    This will actually populate the values. How you chose the article_id is another program's problem.

    I hope you are learning something because people will not write code for you. You have to understand and play with your code. If you do take code from someone else, talk to that person because they can best help you.

    Good luck!

  10. #10
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't expect anyone to write code for me. I'm experienced with ASP and I know how this should be done but it's just that I'm having problems getting it done.

    Thanks for your help I'm learning a lot.

  11. #11
    SitePoint Zealot Hulkur's Avatar
    Join Date
    Oct 2001
    Location
    Estonia
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $pages[]*=*'<a href="http://www.objeqt.com/maniacs/articles.php?id='.$articles_id.'&'.$nextpage.'">'.
    I didn't see where you call your function, but if quoted part
    is what calls the page, then you set $id not $article_id
    (2B) or (not 2B) = FF

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Hulkur said on your prev/next links you pass the value of $article_id but with the name id so atricle_id is gonna be available to you you could either use id in your query instead of article_id or change your links to pass artcile_id=$article_id instead of id=$article_id
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It must have been that all along. I can't believe it was that simple.
    I coded something completely different because I thought that it couldn't be done.
    Thanks a lot.


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
  •