#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0

    Display Block not showing


    It's a small assignment for class and I've been struggling to keep up and make sense of it all. I have some time to post and ask for help still, so I wanted to do that rather than wait any longer.

    "•Within the script, construct a SELECT statement which will query the "items" table for the items currently listed in the flea market's inventory.
    •Process the resulting recordset with a while loop which will create a separate line for each item. (NOTE: You can use line-break tags.)
    •On each line, display the information which has been saved for each item."

    I know I still have to work on the rest of that, but im looking to understand one thing first, as well as figure out why it isn't being displayed.

    PHP Code:
    <?php
    $db_name 
    "fleamarket";
    $table_name "items";
    //Connect to server
    $connection = @mysql_connect("localhost""script""password")
        or die(
    mysql_error());
    $db = @mysql_select_db($db_name$connection) or die (mysql_error());
    $sql "SELECT itemID, itemPrice, description FROM $table_name ORDER BY itemID";
    $result = @mysql_query($sql$connection) or die (mysql_error());

    $display_block '';

    //start looping
    while ($row mysql_fetch_array($result)) {
    $itemID $row['itemID'];
    $itemPrice $row['itemPrice'];
    $description $row['description'];

    $display_block .= "<p><strong>$itemID</strong><br></br>
        
    $itemPrice<br></br>
        Item Description: 
    $description</p>";
    }
    ?>

    <html>
    <head>
    <title>Items Stocklist </title>
    </head>
    <body>
    <h1>Items Ordered by ID</h1>
    <? echo "$display_block"?>
    </body>
    </html>
    looking around online, I read that i needed the $display_block = ''; before I start a loop, however that isn't listed in anything I've read yet so I didn't know/don't know why. Without it, I get an error saying the $description in the display_block is undefined.

    The other issue now is why it doesn't display. The "Items Ordered by ID" in the HTML displays but then nothing else is displayed. I'm sure it's something simple and basic that I'm leaving out, but I know that I'm new enough that everything isn't sinking in as quickly as I want it to.

    Any help or direction would be greatly appreciated. I aim to check back often as I need to finish this by tomorrow night, but will continue to look at what I might be doing wrong here. Thanks for any advice!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,112
    Rep Power
    9398
    Originally Posted by mudpuppet
    looking around online, I read that i needed the $display_block = ''; before I start a loop, however that isn't listed in anything I've read yet so I didn't know/don't know why. Without it, I get an error saying the $description in the display_block is undefined.
    The line inside the loop says "take the value of $display_block, append a string to it, and save that back into $display_block". The key part of that is "take the value of $display_block" and originates from the use of the .= operator. Without the empty assignment the variable does not exist at all so PHP will complain that it's undefined - $display_block, that is, not $description (I think you misread the error message).

    Originally Posted by mudpuppet
    The other issue now is why it doesn't display. The "Items Ordered by ID" in the HTML displays but then nothing else is displayed. I'm sure it's something simple and basic that I'm leaving out, but I know that I'm new enough that everything isn't sinking in as quickly as I want it to.
    Assuming there is something in the items table, the problem is probably your use of the short open tag <?. For that to work there must be a PHP setting enabled. You can check this by looking at the HTML source of the page (ie, View Source). You'll probably see your PHP code there. Instead of short tags use the long <?php version.

    Also, you generally don't need to put quotes around variables.
    PHP Code:
    <?php echo $display_block?>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Thanks for the quick reply!The <?php fixed the display.
    Without formatting, this is how it now looks and displays.

    Items Ordered by ID

    Item ID: 2

    Item Price: 20

    Item Description: US

    Without going to check right now, I assume there is at least 1 entry in the database and this is it being displayed. If there were 3, how would I expect the display to change? I'm going to go check on that to see what is there and to make sure I have whats needed for the assignment but I'm glad that it seems to be working at this point in time.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Okay, I got it displaying and used the old script I had to input more values, and it does in fact work! The last change I wanted to do was to check and see if $itemPrice is less than $salePrice. If so, the ID and price should be in green but if not, then it will display normally.

    The error is on line 22, just after the note to check for sale price. "unexpected T_STRING" so I'm missing something, but not sure what at this point.

    PHP Code:
    <?php
    $db_name 
    "fleamarket";
    $table_name "items";

    //Connect to server
    $connection = @mysql_connect("localhost""script""7111985")
        or die(
    mysql_error());
    $db = @mysql_select_db($db_name$connection) or die (mysql_error());

    $sql "SELECT itemID, itemPrice, description FROM $table_name ORDER BY itemID";
    $result = @mysql_query($sql$connection) or die (mysql_error());

    $display_block '';
    $salePrice "10";
    //Start looping
    while ($row mysql_fetch_array($result)) {
    $itemID $row['itemID'];
    $itemPrice $row['itemPrice'];
    $description $row['description'];
    //Check for sale price, if sale print green, else normal
    if ($itemPrice $salePrice) {
    $display_block .= "<p><font color="green"><b>Item ID: $itemID</b><br></br>
        <b>Item Price:</b> 
    $itemPrice</font>
        <b>Item Description:</b> 
    $description</p><br>";
    } else {
    $display_block .= "<p><b>Item ID: $itemID</b><br></br>
        <b>Item Price:</b> 
    $itemPrice
        <b>Item Description:</b> 
    $description</p><br>";
    }
    ?>

    <html>
    <head>
    <title>Items Stocklist </title>
    </head>

    <body>
    <h1>Items Ordered by ID</h1>
    <?php echo "$display_block"?>
    </body>
    </html>
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,112
    Rep Power
    9398
    PHP Code:
    $display_block .= "<p><font color="green"><b>Item ID: $itemID</b><br></br> 
        <b>Item Price:</b> 
    $itemPrice</font> 
        <b>Item Description:</b> 
    $description</p><br>"
    See how "green" is in a different color? That's syntax highlighting. It should be red like the rest of the string.

    You put double quotes around the color, but the string uses double quotes too. Either you escape the inner quotes so PHP doesn't think you're trying to end the string, or use single quotes which won't conflict with the string's quotes.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Thank you. I changed that and noticed I was also missing a bracket to close it off with. Everything displays properly now and and there are no more errors. Thank you so much for your help! I need to go over the other labs I've done to make sure they are as right as they can be. I feel excited for the second week in a row, and I might actually be catching on some! Now I just need to commit it all to memory, heh.

IMN logo majestic logo threadwatch logo seochat tools logo