/    Sign up×
Community /Pin to ProfileBookmark

if{}else{} multiple querys

This is what I would like to do.

[list]

  • [*]

    issue a query to the guestbook table


  • [*]

    issue a query to the guestbook_comments table


  • [*]

    match the ID from the guestbook table with the ID from the guest_comments table


  • [*]

    put an if{}else{} statement in the $guest_block to check if there is a match between two tables ID numbers


  • [*]

    then using a ‘while {}’ loop create the entries below the initial post from the guestbook table


  • [/list]

    this is the function I am writing to carry out this script. I will make the reply creation script later. (hard stuff first) Right now it errors and just displays a blank page. NO Error MSG displays at all

    [code=php]
    function guest($connection) {
    $table_name = “guestbook”;
    $table_2 = “guest_comments”;
    $r_sql = “SELECT * FROM $table_2 ORDER BY id ASC”;
    $r_result = mysql_query($sql,$connection) or die (mysql_error());
    $sql = “SELECT * FROM $table_name ORDER BY id DESC”;
    $result = mysql_query($sql,$connection) or die (mysql_error());
    global $guest_block;
    $guest_block = “<table class=”gbook” width=”70%” align=”center” cellpadding=”5″ cellspacing=”0″>”;
    //While loop will create an array called $row
    while ($row = mysql_fetch_array($result)) {
    // get individual elements from the array
    $user_no = ($row[‘id’]);
    $name = ($row[‘name’]);
    $date = ($row[‘date’]);
    $e_mail = ($row[‘e_mail’]);
    $msg = ($row[‘msg’]);
    $web_site = ($row[‘web_site’]);
    $guest_block .= ”
    <tr class=”gb_bar”>
    <td colspan=”2″><span class=”title_bar”>$name No: $user_no<br/>$date</span></td>
    </tr>
    <tr>
    <td colspan=”2″ class=”msgblock”><p>$msg</p></td>
    </tr>
    <tr>
    <td colspan=”2″ class=”msgbottom”><span class=”web”><a href=”$web_site” title=”Erie Guestbook”>Favorite Site</a> </span></td>
    </tr>”;
    //This part is a new edition and doesn’t work yet
    if ($result == $r_result) {
    while ($row = mysql_fetch_array($r_result)) {
    // get individual elements from the Reply array
    $r_user_no = ($row[‘r_id’]);
    $r_name = ($row[‘r_name’]);
    $r_date = ($row[‘r_date’]);
    $r_e_mail = ($row[‘r_e_mail’]);
    $r_msg = ($row[‘r_msg’]);
    $r_web_site = ($row[‘r_web_site’]);
    // for testing purposes it only displays the ID for now I will
    //end up displayin all the info soon enough
    $guest_block .= “<tr><td>&nbsp;</td><td>$r_user_no</td></tr>”;

    } else {
    $guest_block .= “<tr><td>&nbsp;</td><td>&nbsp;</td></tr>”;
    }
    }
    $guest_block .= “</table>”;
    }
    [/code]

    And I call the function from this file which has two different functions. One to show the POSTS and FORM and the other function INSERTS the data from FORM and shows just the guest book. this is just the PHP from it since it is the only important part of the document.

    [code=php]
    <?
    require(“listingsfunc2.php”);
    if ($_POST[e_mail] == “”) {
    ini_set(“display_errors”, “1”);
    error_reporting(E_ALL);
    // No FORM must have been submited so moving on…
    // I want to display guestbook entries also a FORM so they can add an entry

    //log the user IP, name of page, User Agent
    $page_title = “Guest Book”;

    dbconnect();
    pagecount($page_title);

    //$guest = Works displays fine all the entrys in the guestbook

    guest($connection);

    //need these variable defined for the form
    $date = date(“Y-m-d”);
    $address = getenv(“REMOTE_ADDR”);
    //start the form that post to this itself
    $form_block = ”
    <table align=”center” cellspacing=”0″ cellpadding=”20″ class=”form”>
    <tr>
    <td>
    <br/><br/><span class=”lineup”>
    <form method=”post” action=”$_SERVER[PHP_SELF]”>

    <input type=”hidden” name=”date” value=”$date” />
    <input type=”hidden” name=”ip” value=”$address” /><br/>

    <label>Name</label><br />
    <input type=”text” name=”name” size=”30″ /><br />

    <label>Favorite Web Site</label><br />
    <input type=”text” name=”web_site” size=”30″ value=”http://www.eriescene.com” /><br />

    <label>E-mail Address</label><br />
    <input type=”text” name=”e_mail” size=”30″ /><br />

    <label>Your Message</label><br />
    <textarea name=”msg” rows=”6″ cols=”40″></textarea><br /><br />
    <input type=”hidden” name=”op” value=”ds” />
    <div align=”center”>
    <input name=”submit” type=”submit” value=”Submit Post” />
    <input name=”reset” type=”reset” value=”Reset” />
    </div>
    </form>
    </span>
    </td>
    </tr>
    </table>”;
    } else {
    ini_set(“display_errors”, “1”);
    error_reporting(E_ALL);
    dbconnect();
    //the form was submited properly because the all the fields are filled in
    //log the user IP, name of page, User Agent
    $page_title = “Guest Book”;
    pagecount($page_title);
    function sanitize($input)
    {
    if(get_magic_quotes_gpc())
    {
    $input = stripslashes($input);
    }
    return(mysql_real_escape_string($input));
    }

    //Start INSERT the Results of the Form
    //Run Sanitize Function
    $name = sanitize($_POST[‘name’]);
    $date = ($_POST[‘date’]);
    $e_mail = sanitize($_POST[‘e_mail’]);
    $msg = sanitize($_POST[‘msg’]);
    $web_site = sanitize($_POST[‘web_site’]);

    //start, build, and issue the QUERY statment
    $table_name = “guestbook”;
    $sql = ”
    INSERT INTO $table_name VALUES ( ”, ‘$name’, ‘$date’, ‘$e_mail’, ‘$msg’, ‘$ip’, ‘$web_site’)”;
    $result = mysql_query($sql,$connection)
    or die (‘I cannot connect to the database because: ‘ . mysql_error());

    //Connect again to view the database after the new values have been inserted
    //Remeber no form this time they allready posted.
    guest($connection);
    $form_block = “<div align=”center”><span style=”color:green;font-size:12px;”>Thanks for Posting!!</span></div>”;

    }
    //close the db connection.}
    mysql_close($connection);
    ?>
    [/code]

    Like I said I started this and ran it with no success. Can anyone help me with this? I don’t really even know if this is completely possible the way I percieve it.

    to post a comment
    PHP

    16 Comments(s)

    Copy linkTweet thisAlerts:
    @Markbad311authorJan 31.2006 — forgot to close the first while loop changes made above.
    Copy linkTweet thisAlerts:
    @chazzyJan 31.2006 — you realize that you can join tables in a sql query right?

    you shouldn't be doing something like this at the application level.
    Copy linkTweet thisAlerts:
    @Markbad311authorJan 31.2006 — well how do I do it that way? my PHP book has limited MySQL instructions in it. And how will I put it in the while loop (because it will be in a different type box so visually you know it is a comment)
    Copy linkTweet thisAlerts:
    @Markbad311authorJan 31.2006 — See how they are two different orders? I kinda would lie it to be so the posts are listed newest to oldest and the comments are listed from oldest to newest.

    [code=php]
    $r_sql = "SELECT * FROM guestbook_comments ORDER BY id ASC";
    $r_result = mysql_query($sql,$connection) or die (mysql_error());
    $sql = "SELECT * FROM guestbook ORDER BY id DESC";
    [/code]


    [COLOR=Blue]here is a [/COLOR][URL=http://www.eriescene.com/drinking_games.html]VISUAL REPRESENTATION[/URL] [COLOR=Blue]of what I am trying to accomplish.[/COLOR]



    thanks
    Copy linkTweet thisAlerts:
    @chazzyJan 31.2006 — Ok, misunderstood what you are doing.

    You should do something only select the correct data, not go through everything. There should be a linking key/foreign key defined on your tables, what is it?
    Copy linkTweet thisAlerts:
    @Markbad311authorJan 31.2006 — well my guestbook table the primary key is id and it is auto increment giving each post its own number and the guestbook_comments table has a primary key called r_id and it is *NOT* auto incrementing. figured as the replys were posted then I would take the id from guestbook table and insert it in to r_id so I would then have the relation between which reply got to which post.
    Copy linkTweet thisAlerts:
    @chazzyJan 31.2006 — r_id shouldn't be a primary key, if multiple comments to each "guestbook". is it a 1-1 correspondence? why split it over 2 tables?

    so in theory you could do

    <i>
    </i>SELECT
    (put the applicable columns here)
    FROM
    guestbook g, guestbook_comments c
    WHERE
    g.id = c.r_id;
    Copy linkTweet thisAlerts:
    @Markbad311authorFeb 01.2006 — well I am not sure what 1-1 correspondence means to you but to me what your asking is if ID=1 in guestbook and certain comments in guestbook_comments will have an ID equal 1 providing a relation between the two. that query above would only select guetbook entrie with comments not the ones without. I need a way with what I have set up allready to query both tables and select all the fields in both tables the to process them so that (inside of the while loop) it will show all the comments (more then one usually) directly under the post. that matches id and r_id. Once the comments are done it starts back at the top of the loop and continues to display single posts until the post has a comment again... thats what I have been trying to do above

    The ID in the Guestbook_comments table will be inserted in to the table from the corresponding Guestbook table entry when the user submits the form. (that i will generate later once these bugs are gone.)
    Copy linkTweet thisAlerts:
    @Markbad311authorFeb 01.2006 — in this code snippet out of the above published code.

    [code=php]
    while ($row = mysql_fetch_array($result)) {
    // get individual elements from the array
    $user_no = ($row['id']);
    $name = ($row['name']);
    $date = ($row['date']);
    $e_mail = ($row['e_mail']);
    $msg = ($row['msg']);
    $web_site = ($row['web_site']);
    $guest_block .= "
    <tr class="gb_bar">
    <td colspan="2"><span class="title_bar">$name No: $user_no<br/>$date</span></td>
    </tr>
    <tr>
    <td colspan="2" class="msgblock"><p>$msg</p></td>
    </tr>
    <tr>
    <td colspan="2" class="msgbottom"><span class="web"><a href="$web_site" title="Erie Guestbook">Favorite Site</a> </span></td>
    </tr>";
    ///////////////////////////
    //Should I be assigning variables to the array before here?
    if ($id == $r_id) {
    while ($row = mysql_fetch_array($r_result)) {
    // get individual elements from the Reply array
    $r_user_no = ($row['r_id']);
    $r_name = ($row['r_name']);
    $r_date = ($row['r_date']);
    $r_e_mail = ($row['r_e_mail']);
    $r_msg = ($row['r_msg']);
    $r_web_site = ($row['r_web_site']);
    $guest_block .= "<tr><td>&nbsp;</td><td>$r_user_no</td></tr>";
    } else {

    $guest_block .= "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
    }
    }
    $guest_block .= "</table>";
    [/code]


    Should I be assigning variables to the fields in the array before that [B]If[/B] statement? and do like a loop like this section inside of the exsisting loop? because the way I see it. it has to be in that while loop that is displaying the first initial post.

    [code=php]
    if (id == r_id) {
    while(id == r_id) {
    // add to the table for the reply to the post
    $guest_block .= "<tr><td>$r_user_no, $r_msg, </td><td></td></tr>";
    }

    } else {
    $guest_block .= "<tr><td colspan="2">$nbsp;</td></tr>";
    }
    [/code]
    Copy linkTweet thisAlerts:
    @chazzyFeb 02.2006 — where are you defining r_id?

    anyways, this is a pseudocode of what you should do:

    do_query(SELECT * FROM guestbook ORDER BY id DESC)

    for each row in the resultset RS:

    //whatever printing needs to be done goes here

    do_query(SELECT *
    FROM guest_comments WHERE id=RS.id ORDER BY id ASC)

    for each row in the resultset RSC:

    //do whatever else

    end for;

    end for;
    Copy linkTweet thisAlerts:
    @Markbad311authorFeb 03.2006 — what is a result set? is that the variable I hold the result in?

    r_id is a field name of the table.

    if id == r_id then I print the portion of the comment to the initial post. I don't understand the result set RS or RSC. I am not familiar with it.
    Copy linkTweet thisAlerts:
    @Markbad311authorFeb 04.2006 — < bump > this topic is very important to me and I am anxiously awaiting any and every bit of help i can and have recieved.
    Copy linkTweet thisAlerts:
    @chazzyFeb 04.2006 — resultset is what you get back when you issue a query.

    rs and rsc are just names i gave to the different result sets, like this:

    $handle = mysql_connect();

    $sql = "SELECT * from whatever..";

    $resultset = mysql_query($sql);

    while($row = mysql_fetch_array($resultset)){

    //do something

    }
    Copy linkTweet thisAlerts:
    @Markbad311authorFeb 04.2006 — thanks a bunch Chazzy I think I should have enough information to proceed with this project now.
    Copy linkTweet thisAlerts:
    @Markbad311authorFeb 05.2006 — ok used your advice ( it think I followed the directions ) But this is my code

    and I am still gettin a blank page. I have probally overlooked something. Can you help me find it? Here is the Function out of my Function Library.

    [code=php]
    function guest($connection) {
    // Some reason I always have to run things global.
    //They never return = $guest_block
    global $guest_block;
    // table select for original posts
    $table_name = "guestbook";
    // Comments on original posts
    $table_2 = "guest_comments";
    // top of the table
    $guest_block = "<table class="gbook" width="70%" align="center" cellpadding="5" cellspacing="0">";

    $sql = "SELECT * FROM $table_name ORDER BY id DESC";
    $result = mysql_query($sql, $connection) or die (mysql_error());
    for each($row = mysql_fetch_array($result)) {
    // get individual elements from the array
    $user_no = ($row['id']);
    $name = ($row['name']);
    $date = ($row['date']);
    $e_mail = ($row['e_mail']);
    $msg = ($row['msg']);
    $web_site = ($row['web_site']);
    $guest_block .= "
    <tr class="gb_bar">
    <td colspan="2"><span class="title_bar">$name No: $user_no<br/>$date</span></td>
    </tr>
    <tr>
    <td colspan="2" class="msgblock"><p>$msg</p></td>
    </tr>
    <tr>
    <td colspan="2" class="msgbottom"><span class="web"><a href="$web_site" title="Erie Guestbook">Favorite Site</a> </span></td>
    </tr>";
    }
    // Checkin original posts against r_id field in the comment table.
    //$user_no is really just easy to remeber name for ID number of
    //the original post
    $sql2 = "SELECT r_id FROM $table_2 WHERE $user_no = r_id ORDER BY id ASC";
    $r_result = mysql_query($sql, $connection) or die (mysql_error());
    for each($row = mysql_fetch_array($r_result)) {
    // get individual elements from the Reply array give em a good name.
    $r_user_no = ($row['r_id']);
    $r_name = ($row['r_name']);
    $r_date = ($row['r_date']);
    $r_e_mail = ($row['r_e_mail']);
    $r_msg = ($row['r_msg']);
    $r_web_site = ($row['r_web_site']);
    $guest_block .= "<tr><td>&nbsp;</td><td>$r_user_no</td></tr>";
    // Somewhere in this loop I need to define a row to show if there is not
    // a comment. if{}else{} maybe?
    }
    end for;
    end for;
    // Close up the table
    $guest_block .= "</table>";
    }
    [/code]



    I believe there is another mysql function like mysql_num_rows or something like that? how do I utilize this so I can maybe just provide a guest_comments count matching the Original Post and a link. Then on a seperate page, show the original post only and the comments that follow. then I will put the form to reply on that page. Just a thought.

    I am a little confused on the 'for each' command. but doesn't a while l00p do the same thing?
    Copy linkTweet thisAlerts:
    @chazzyFeb 05.2006 — you shouldn't use for each
    [code=php]
    for each($row = mysql_fetch_array($result)) {
    [/code]


    it's a while loop and only a while loop, as recommended to get through the result set. the foreach loop looks at the current value and does that (so it looks at what's in $row and handles $row after it's assigned.) you can do this though:

    [code=php]
    while($row=mysql_fetch_array($result)){
    foreach($row as $item){
    //do stuff with each item
    }
    }
    [/code]


    Edit: this is my fault for not clarifying, pseduocode isn't real code, and it doesn't necessarily translate into any direct real code. the names, functions, etc are all whatever i want to make them, since i wrote the pseudocode. it's supposed to be a generalized "how does this body of code work."


    // Somewhere in this loop I need to define a row to show if there is not

    // a comment. if{}else{} maybe?
    [/quote]


    If you look at how i did the code, you'd notice that it's a nested loop. you have:

    <i>
    </i>loop1{
    //
    }
    loop2{
    //
    }


    when it needs to look like this:

    <i>
    </i>loop1{
    //
    loop2{
    //
    }
    //
    }


    where the // in my code represent missing lines of code. Do you understand how these two structures differ?
    ×

    Success!

    Help @Markbad311 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...