PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2013, 06:11 PM
mudpuppet mudpuppet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 23 mudpuppet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old February 23rd, 2013, 06:44 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 54 m 57 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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?>

Reply With Quote
  #3  
Old February 23rd, 2013, 07:43 PM
mudpuppet mudpuppet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 23 mudpuppet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old February 23rd, 2013, 10:00 PM
mudpuppet mudpuppet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 23 mudpuppet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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>

Reply With Quote
  #5  
Old February 23rd, 2013, 10:05 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 54 m 57 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.

Reply With Quote
  #6  
Old February 23rd, 2013, 10:14 PM
mudpuppet mudpuppet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 23 mudpuppet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Display Block not showing

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap