PHP/MySQL: How to generate full pages from database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
InternationalMangaMan
Forum Newbie
Posts: 6
Joined: Wed Sep 03, 2003 8:53 am
Location: Sydney, Australia

PHP/MySQL: How to generate full pages from database

Post by InternationalMangaMan »

Sorry, title sounds a bit lame....

Wondering if anyone can help me. I know basics and want to learn, but I'm a bit stumped. I know you can do it, just not sure how....

What I want to do is have a menu. If you press button 1, it opens a new page that displays all of the fields in database row number 1 - press button 2, opens info from line 2 - and so on. Is it maybe something to do with the URL and putting in a variale somewhere? I don't know. If my description is a little vague, I'll give an example.

Database:

Line 1: Fred - Car - 5 years
Line 2: John - Bike - 1 year
Line 3: Mike - Skateboard - 15 years

Press the "Car" button on the first page and a new page opens displaying Fred has been using his Car for 5 years. Press the "Bike" button and you get John has been using his Bike for 1 year. Sorry, I'm prolly giving too much info.

I guess the crux is that I want the new page that opens to be able to read the line number based on what was pressed previously. Do I have to write to the database on the first page and then read it in the second? Or is there an easier way to transfer variable values to the new page ??

Please help, I do want to learn......
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

have you worked through any tutorials?

http://www.phpcomplete.com/tutorials.ph ... adTutorial
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Basic theory: give every database row it's own unique identifyer, so when you want to pull the page from your database, you can add a simple WHERE pageid="$_GET['pageid']" in your sql statement so you can grab page id's from the URL similar to how this forum does it.

I suggest reading a good tutorial if the theory of it all seems a but impossible to fully grasp.

Good luck.
InternationalMangaMan
Forum Newbie
Posts: 6
Joined: Wed Sep 03, 2003 8:53 am
Location: Sydney, Australia

Post by InternationalMangaMan »

Okay, firstly, thanks for answering.

I had a quick look at the tutorials, but they all seem the basic stuff. I am more than confident with connecting, retrieving, and sorting data.

I guess what I really want to do is have a page that shows a gallery of pictures. I would like for that gallery to be change each week to view a different person's pictures. I also want to be able to view previous weeks galleries.

Hmmm..... here's a thought. Maybe have a table set up that has:

!- url -!- week 1 picture -!- week 1 blurb -!- brief description -!
!- url -!- week 2 picture -!- week 2 blurb -!- brief description -!
...and so on.

Then, I could (from the main page) access the first field and open that page and fill it with the info. But that would mean a whole bunch of files defeating the purpose of a data base......

Okay, how about just setting a number that carries over to a new page which then accesses the line in the database to fill the page with information. That way I could click on a description to view previous entries.... how would I do that?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well, Sami was on to something (if I understood it correctly).

If you add a field (id, INT), and access it using the URI (.../index.php?id=2) , you can use $_GET['id'] to the the '2' and you then know "select * from table where id = '2'" (basicly).

You can also add multible selections in the URI as example: ".../index.php?id=2&week=42&foo=bar" to fit your needs.
InternationalMangaMan
Forum Newbie
Posts: 6
Joined: Wed Sep 03, 2003 8:53 am
Location: Sydney, Australia

Post by InternationalMangaMan »

Okay, okay, I think I'm getting it now......

So in the simpliest form, set up a link to "../nextpage.php?id=2"

then in the new page, do the query select * where id = "$_get['id']"

I think that about covers it.

Thanks everyone. Once I get it working, I might even post the code here so that others can benefit from it.
InternationalMangaMan
Forum Newbie
Posts: 6
Joined: Wed Sep 03, 2003 8:53 am
Location: Sydney, Australia

Post by InternationalMangaMan »

Okay, further to this problem...... I have worked out how to do this and I've now got it wired...... BUT!

I need the actual command to get the variable from the URL. So, this is the code I have at the moment.... it's very simple, but illustrates the idea.

Code: Select all

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM TestTable WHERE id=2";
$result=mysql_query($query);

$id=mysql_result($result,0,"id");
$name=mysql_result($result,0,"name");
$age=mysql_result($result,0,"age");

echo "<tr>	<td align=center valign=top colspan=3>$id...$name...$age</td></tr>";

mysql_close();
So, where I have written id=2, I want it to be id = whatever the variable is in the URL (if, for example, the URL was ..../GetTest.php?ln=4, then the code should be id=ln....)

Can somebody help me !!! I understand that my server is runnin PHP 4.2....something, I've tried the $_GET(whatever) command, but it didn't work.

Any thoughts ??
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

First, check to make sure you're getting the variables passed like you think you are.

Code: Select all

$ln = $_GET["ln"];
echo $ln;
Assuming that's working, all you should need to do is set up the query.

Code: Select all

$query="SELECT * FROM TestTable WHERE id=$ln";
Should work.
InternationalMangaMan
Forum Newbie
Posts: 6
Joined: Wed Sep 03, 2003 8:53 am
Location: Sydney, Australia

Post by InternationalMangaMan »

That's exactly what I tried, but it didn't work. It would assign the $_GET(whatever) to another variable. I don't know why, maybe it's the version of php.

Is there another way?
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

What does it echo for the value of $ln? Are you sure that you're using GET to access this page? You might be sending the variables by POST. $_REQUEST["ln"] will detect either method, but I imagine it's somewhat slower.
InternationalMangaMan
Forum Newbie
Posts: 6
Joined: Wed Sep 03, 2003 8:53 am
Location: Sydney, Australia

Post by InternationalMangaMan »

Okay, hold up, stop the press. It's working. I'm having some problems with the refreshing of the page...... but, it is working.

So, here's the code with additions. Thanks to Unipus !!

Code: Select all

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$ln=$_GET&#1111;"ln"];
$query="SELECT * FROM TestTable WHERE id=$ln";
$result=mysql_query($query);

$id=mysql_result($result,0,"id");
$name=mysql_result($result,0,"name");
$age=mysql_result($result,0,"age");

echo "<tr>	<td align=center valign=top colspan=3>$id...$name...$age</td></tr>";

mysql_close();
And that's using the URL : http://www.internationalmanga.com/GetTest.php?ln=3

Thanks again !!
Post Reply