...

View Full Version : Resolved Display from database



Craigy
06-16-2010, 11:54 PM
I am wanting to fill parts of a page with values from a mysql database but Im not sure how to go about doing it. I used an example from w3 schools which displays things in a table fine, but I am wanting a different layout Please can somebody point me in the right direction. The layout works fine with
<?php include("news.php"); ?> without my attempt to add database values.


<?php
$username = "user";
$password = "password";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("username_news",$dbhandle)
or die("Could not select username_news");
echo "Connected to Database<br>";

//execute the SQL query and return records
$result = mysql_query("SELECT title, newstext, id, date FROM news");
echo "queried<br>";

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {

echo "<div id="main_column">"
echo "<div class="main_column_section">"

echo "<h2><span></span>".$row{'title'}. "</h2>"

echo" <div class="main_column_section_content">"

echo" <a href="#"><img class="image_wrapper fl_image" src="images/image_01.jpg" alt="image one" /></a>"
echo" <p><br />"
echo "<br />"
echo"".$row{'newstext'}. "<br />"
echo"".$row{'id'}. "<br />"
echo"".$row{'date'}. "<br />"



echo" </p>"
echo" </div>"
echo" <div class="cleaner"></div>"
echo" <div class="bottom"></div>"
echo"</div>"
}
//close the connection
mysql_close($dbhandle);
?>

Jazz914
06-17-2010, 01:27 AM
Where you have your while replace that code with the following:

<?php
echo('<div id="main_column">');
echo('<div class="main_column_section">');
echo('<h2><span></span>'.$row['title'].'</h2>');
echo('<div class="main_column_section_content">');
echo('<a href="#"><img class="image_wrapper fl_image" src="images/image_01.jpg" alt="image one" /></a>');
echo('<p><br />');
echo('<br />');
echo($row['newstext']. "<br />");
echo($row['id']. "<br />");
echo($row['date']. "<br />");
echo('</p>');
echo('</div>');
echo('<div class="cleaner"></div>');
echo('<div class="bottom"></div>');
echo('</div>');
?>

You left a lot of unclosed echo's (";") and to save the long method of escaping quotes with backslashes I replaced them with single quotes, take a look at the following example:


<?php echo("<div id="main_column">"); ?>
Then compare that with:

<?php echo('<h2><span></span>'.$row['title'].'</h2>'); ?>

If you notice in the second example you close the quote to allow for a php variable to be added into the echo, well this is exactly what you're doing with the first one except php thinks that "main_column" is meant to be php code because the first quote is being closed by the div ID quote.

and if you wanted to keep the double quotes for personal preference:
You have the following code:

<?php echo("<div class="bottom"></div>"); ?>
but php keeps closing the first quote and causing errors, simply escape any double quotes within the SAMe quotes with a backslash before the quote like so:

<?php echo("<div class=\"bottom\"></div>"); ?>
Now PHP would process this correctly.


Edit:

Also take a look at the syntax highlighting for a better example

Craigy
06-17-2010, 10:13 AM
Wow works very well indeed. I found

echo $var; // Results in the value of $var being printed
echo '$var'; // Results in the word '$var'
echo "$var"; // Results in the value of $var being printed

Which I will keep handy. Thank you for pointing me in the right direction to learn something new.

The only problem now is it isn't populating the space with the database entry.

echo('<a href="#"><img class="image_wrapper fl_image" src="images/image_01.jpg" alt="image one" /></a>');

Displays the image. Perfectly and is great.

But

echo('<h2><span></span>'.$row['title'].'</h2>');
Does not call in the title value to populate the top, it is just a blank space.

I tried

echo('<h2><span></span>' ".$row['title']." '</h2>');
since
echo "$var"; // Results in the value of $var being printed
but that just breaks the whole thing again.:(

tomws
06-17-2010, 03:42 PM
But

echo('<h2><span></span>'.$row['title'].'</h2>');
Does not call in the title value to populate the top, it is just a blank space.


If you see the h2 and span tags when viewing the page source, that means there is no value for $row['title'] (or it may be zero, whitespace, or other equivalently empty() text).

Jazz914
06-17-2010, 05:08 PM
What is the value of title in the table?

Craigy
06-17-2010, 08:58 PM
Sorry I'm still struggling :(

database name : username_news
table name : news

http://img294.imageshack.us/img294/1748/27570138.png

http://img175.imageshack.us/img175/7895/15440899.png

tomws
06-17-2010, 09:20 PM
Show your current code. I suspect you indiscriminately copied/pasted jazz914's code and it's in the wrong place. Specifically, I'll bet you're missing your mysql_fetch_array line now.

Craigy
06-17-2010, 10:09 PM
You were right that's what I was missing, after reading

http://php.net/manual/en/function.mysql-fetch-array.php

I have added

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

jazz914's code

}


and it now works perfectly.

Thank you for pointing me in the right direction and all the help. I really do appreciate it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum