Forum Moderators: coopster

Message Too Old, No Replies

PHP/SQL Noob Needs Help!

         

Phillips126

2:46 am on Jul 31, 2009 (gmt 0)

10+ Year Member



Hey guys,

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]

eelixduppy

3:10 am on Jul 31, 2009 (gmt 0)



Hello and Welcome to WebmasterWorld!

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. :)

Phillips126

11:00 pm on Jul 31, 2009 (gmt 0)

10+ Year Member



Alright, gave it a shot and not making much progress.
Here is what I have... try not to laugh too hard :)

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?

Phillips126

1:52 am on Aug 1, 2009 (gmt 0)

10+ Year Member



Ok, is this making any progress or am I just destroying it further?


<?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>";
?>

StoutFiles

2:32 am on Aug 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
//you have to connect to mysql first. Your mysql connection will have a username and password.
mysql_connect("localhost", "your_username", "your_password") or die(mysql_error());

//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 />";

}
?>

Phillips126

3:23 am on Aug 1, 2009 (gmt 0)

10+ Year Member



Woohoo!

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'); ?>

Phillips126

2:31 pm on Aug 1, 2009 (gmt 0)

10+ Year Member



Hmm... Still cant seem to get the ratings working.

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!

Phillips126

1:38 am on Aug 2, 2009 (gmt 0)

10+ Year Member



Anyone have any ideas? Can't really progress any further with this website until I can get this figured out.

Thanks

Phillips126

4:03 am on Aug 2, 2009 (gmt 0)

10+ Year Member



Nevermind, was able to figure it out myself. Thanks for those who helped previously.

eelixduppy

6:20 am on Aug 2, 2009 (gmt 0)



Cool. :)