Forum Moderators: coopster
Im trying to learn PHP / mySQL and as all beginners am having some trouble.
I am attempting to create a database, and dynamically pull the data from the SQL database and place it in the designated areas on my page.
I have attached an image which helps illustrate what I am trying to do. I'm not asking for the entire script to be written, as that wouldn't really teach me anything... but if I can get a push in the right direction, or some idea as how to begin this, that would be awesome.
<snip>
The image on top shows the SQL Fields I want to include. Below that is an image to the current HTML, which is producing the look on the bottom. I figured the HTML code could help in producing the PHP version.
Here's the HTML:
<div id="left_column_website" class="row_hover">
<div id="website_image" class="galleryimage">
<a href="#"><img src="images/webthumbs/test.jpg" width="150" height="65" /></a></div>
<div id="website_info"><span class="website_class"><a href="#">Website Name</a></span><br />
<hr id="hr" />
Date Added: 7/26/09<br />
Website Type: HTML/CSS<br />
<?php echo rating_bar('Test1','5'); ?>
</div>
</div>
Browsing these forums, I've noticed you guys really know your stuff and figured no better place to ask for help!
Thanks so much, I'll keep returning to this topic to give updates and ask more questions if needed.
[edited by: eelixduppy at 3:05 am (utc) on July 31, 2009]
[edit reason] no screenshots, please [/edit]
Here is a thread from our library that might help get you on your way: [webmasterworld.com...]
Try to come up with something from that to have as a basis for your learning process and we can take it from there.
Please note that even though that thread does not cover it, it is certainly of great concern. When you are done learning the basics read up on how mysql_real_escape_string [php.net] can protect your queries.
Good luck. :)
Problem now is that I am getting blank fields. I'm sure that when I escaped the extra quotes ("") it is now not reading the script correctly. Atleast it is going to the page and not giving me a database error anymore haha.
How should this be typed to be in correct syntax?
<?php
echo "<div id=\"left_column_website\" class=\"row_hover\">";
echo "<div id=\"website_image\" class=\"galleryimage\">";
echo "<a href=\"$web_url\"><img src=\"$web_image\" width=\"150\" height=\"65\" /></a></div>";
echo "<div id=\"website_info\"><span class=\"website_class\"><a href=\"$web_url\">$web_name</a></span><br />";
echo "<hr id=\"hr\" />";
echo "Date Added: $web_date<br />";
echo "Website Type: $web_type<br />"; ?>
<?php echo "rating_bar('$web_name','5')"; ?>
<?php echo "</div>";
echo "</div>";
?>
EDIT: Oh, just realized I dont have the database being "Queried..." is that the problem?
<?php
$result = mysql_query("SELECT * FROM websites") or die(mysql_error());
echo "<div id=\"left_column_website\" class=\"row_hover\">";
echo "<div id=\"website_image\" class=\"galleryimage\">";
echo "<a href=\"",$web_url,"\"><img src=\"",$web_image,"\" width=\"150\" height=\"65\" /></a></div>";
echo "<div id=\"website_info\"><span class=\"website_class\"><a href=\"",$web_url,"\">$web_name</a></span><br />";
echo "<hr id=\"hr\" />";
echo "Date Added: ",$web_date,"<br />";
echo "Website Type: ",$web_type,"<br />"; ?>
<?php echo "rating_bar('",$web_name,"','5')"; ?>
<?php echo "</div>";
echo "</div>";
?>
//you have to enter the name of your database.
mysql_select_db("your_database_name") or die(mysql_error());
//"websites" needs to be the name of your table
$result = mysql_query("SELECT * from websites")
or die(mysql_error());
//this will loop printing out every row of the table "websites"
while($row = mysql_fetch_array($result))
{
//when inserting data, specify the title of the column using the $row[''] command.
//$row['this_needs_to_be_the_title_of_the_column_you_are_referencing'];
//example lines
echo "Date Added: ".$row['web_date']."<br />";
echo "Website Type: ".$row['web_type']."<br />";
}
?>
Working beautifully! Thank you so much.
I do have one problem though, with an entirely different table called 'ratings' which controls the voting option.
This is the code:
<?php require('_drawrating.php'); ?>
<?php echo rating_bar($row['web_name'],'5'); ?>
When I add a new website (row) in the websites table, a new Div appears and the newly added 'web_name' gets recorded into the 'ratings' database for vote tracking.
For some reason, when a star is clicked (to attempt to vote) it does not work. However, when I go into the database and manually enter values such as: vote_amount as 5 and vote_value as 3, the correct amount of stars light up indicating a working database...
If I change:
<?php echo rating_bar($row['web_name'],'5'); ?>
To:
<?php echo rating_bar('name-that-matches-particular-database-field-entry','5'); ?>
It works perfectly.
Any ideas what the problem could be?
Thanks!
EDIT: After studying the source code, ect.. it appears that the part $row['web_name'] of the code below is not being read. Is this not right?
<?php echo rating_bar($row['web_name'],'5'); ?>
Here is my complete code (without the database connection, etc...):
<?php
$result = mysql_query("SELECT * FROM websites ORDER BY web_date DESC") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$num = $row['web_name']; echo "<div id=\"left_column_website\" class=\"row_hover\">";
echo "<div id=\"website_image\" class=\"galleryimage\">";
echo "<a href=\"".$row['web_url']."\"><img src=\"".$row['web_image']."\" width=\"150\" height=\"65\" /></a></div>";
echo "<div id=\"website_info\"><span class=\"website_class\"><a href=\"".$row['web_url']."\">".$row['web_name']."</a></span><br />";
echo "<hr id=\"hr\" />";
echo "Date Added: ".$row['web_date']."<br />";
echo "Website Type: ".$row['web_type']."<br />";
echo rating_bar($num,'5');
echo "</div>";
echo "</div>";
}
?>
It appears the problem is that $num from the code below isn't working.
echo rating_bar($num,'5');
Any ideas?
Thanks in advance!