Click to See Complete Forum and Search --> : I'm brand new to PHP. Simple question (I think)


juicemousezero
05-13-2005, 10:56 AM
I spent all day yesterday reading tutorials on PHP and MySQL and I've been enjoying it very much, but there's a very simple matter that still alludes me and I can't seem to find an answer no matter what I read and I just need some help getting started.

Alright, I've got my connection working and everything, but all I've seen how to do so far is make a loop and display all my selected results. As useful as that is (not so much), what I need to do is select one record at a time and display different content on a page according to the record I pick. An example is here at good ol Penny Arcade:

http://www.penny-arcade.com/view.php3?date=2005-05-13

They have a single view.php page, but the content of the page changes based on the date they select from their database. That's what I need to do also, I just don’t know quite how to go about it aside from retrieving data, which I can do. I've got a little archive that's getting bigger every week and pretty soon I'll be buried in HTML pages if I don’t work out another system (you can see the page in question here http://www.juicemousecypher.com/bow.html if you want. It's .html right now but I'll change it once I get everything working). From what I read this isn't really that hard... I just need some gaps filled in and at this point I'm getting pretty frustrated cause I cant find anything that will fill those gaps. So any help at all would be lovely. Thanks in advance.

Natcon67
05-13-2005, 11:57 AM
To me it looks like the penny-arcade.com page is doing a query on the db based on the date in the url.

something like this:

$result = mysql_query("SELECT picture, description FROM cartoons WHERE date = '".$_GET['date']."'");

now that you have the picture (or text or whatever is stored in the db) you can use a mysql_fetch_object to display the information however you want.

juicemousezero
05-13-2005, 05:33 PM
To take it just a step further, how would I implement the mysql_fetch_object function? Thanks for the help and thanks for bearing with me.

Natcon67
05-13-2005, 07:22 PM
this would be a good place to start for you...
http://us2.php.net/manual/en/function.mysql-fetch-object.php

Stephen Philbin
05-13-2005, 07:35 PM
They may even use something much more simple. Something like


<img alt="" src="../strip_images/<?php echo $_GET['date']; ?>.jpg">


That way, it's just the same page each time, it's just the src attribute of the image element that gets tweaked each time. They could just draw their stip and then save it as the publish date and stick it in the strip_images folder. It'd also save thier database server a fair amount of work too.

Oh and for just about any php needs you may have, http://www.zend.com/manual/ should cover it.

neur0maniak
05-14-2005, 05:39 AM
before you go displaying images with a variable for it's filename, you ought to use a file_exists($filename) function to see if it's really there. That'll give you the opportunity to display an image with an error message instead of a broken image.

juicemousezero
05-14-2005, 10:48 AM
Thanks a ton guys. I'll read over those links. Unfortunately I'm displaying different bodies of text instead of images but I'm hoping I'll be able to tweak my way through it. I figure if I can work through this I'll have a decent handle on PHP for future use, and i'll have a more efficient site. Or at leats a site that looks cool cause it uses querystrings.

juicemousezero
05-14-2005, 01:19 PM
Ok guys I'm getting close to working this out. What I'm not sure how to do now is dynamically change the variables that are storing my DB columns which I know I can do with querystrings, but I'm still fuzzy as to how they work (I tried to but I couldn't find anything about them in the daunting manual).

Test pages: http://www.juicemousecypher.com/untitled.php4

As you can see, no matter which link you click it takes you to only the first record in the DB.

Here's the code for untitled.php4


//all code before this is just misc connection stuff
$query = "SELECT * FROM db ORDER BY id DESC";
$result = mysql_query($query);
$rows = mysql_num_rows($result);

mysql_close();

//list the links
$i = 0;
while ($i < $rows)
{
$id = mysql_result($result, $i, "id");
$name = mysql_result($result, $i, "name");
$displayname = mysql_result($result, $i, "displayname");
$content = mysql_result($result, $i, "content");

//list the results
echo "<a href=view.php4?id=$id&name=$name>$displayname</a><br>";
$i = $i +1;
}

?>


and here's the code for the view.php4 page


//all code before this is just misc connection stuff
$query = "SELECT * FROM db";
$result = mysql_query($query);

mysql_close();

$i = 0;

$id = mysql_result($result, $i, "id");
$name = mysql_result($result, $i, "name");
$displayname = mysql_result($result, $i, "displayname");
$content = mysql_result($result, $i, "content");

//print the specified displayname and content records
echo "<center>$displayname<br><br>$content</center>";
?>


Forgive the code if it's not that good. First time and all. So I guess the question in so many words is, how do I dynamically get my selected id and name records from the navagation bar to the $id and $name variables so that the $displayname and $content variables on the view.php4 page know which records to display? Could I use the _GET method for this?

Thanks again. Time to hit the books again. I'm determined to get this working soon.

Natcon67
05-14-2005, 01:35 PM
What you need to do now is filter your query on the view.php4 page to only get the name you specified in the previous link.
make the query look like this:

$query = "SELECT * FROM db WHERE displayname = '".$_GET['name']."'";

Natcon67
05-14-2005, 01:38 PM
By the way, I found these (http://www.zend.com/php/beginners/index.php) tutorials to be helpful.

juicemousezero
05-14-2005, 02:06 PM
IT WORKS!!!! I really can't thank you enough, man. Cheers. Big time cheers.