The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Display Block not showing
Discuss Display Block not showing in the PHP Development forum on Dev Shed. Display Block not showing PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 23rd, 2013, 06:11 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 23
Time spent in forums: 8 h 32 m 54 sec
Reputation Power: 0
|
|
|
PHP-General - 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!
|

February 23rd, 2013, 06:44 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | 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).
Quote: | 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; ?>
|

February 23rd, 2013, 07:43 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 23
Time spent in forums: 8 h 32 m 54 sec
Reputation 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.
|

February 23rd, 2013, 10:00 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 23
Time spent in forums: 8 h 32 m 54 sec
Reputation 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>
|

February 23rd, 2013, 10:05 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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.
|

February 23rd, 2013, 10:14 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 23
Time spent in forums: 8 h 32 m 54 sec
Reputation 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|