Calling all PHP and MySql experts

Status
Not open for further replies.

murdocsvan

Web Programmer
Messages
896
Location
Surrey, UK
I am trying to make a website where the content is based on Mysql database tables. Basically the content is a MySql table of names, which in turn are requested by the PHP on the page and put into an <a href> link. The link in turn, is itself a database query. This then links to the next page and is intended to fill a similar HTML table on the following page with Names in the same way which are turned into links with PHP coding. This process repeats itself several times.

However...

What i need it to do is populate the Following page with links in the same way as the previous page BASED on what page the user was previously on.

I've thought about it hard and the closest thing i can find to a solution is using the PHP Session command.

The way this would work is when a page is loaded, it takes the session written by the previous page, and loads the HTML table based on that session. it then writes to the session about the current table being viewed for the next page to read. this repeats itself as you go further down the tree.

can anyone tell me if im approaching this in a wrong way?
 
I had a difficult time understanding what exactly you want to do.

So, say you have 3 names in the database; Bob, Mark, and Dave.

Your first page displays each name with a link. You click on Bob, and go to another page displaying more information about Bob?

If that ^^ is sort of what you want, then you can add ID's to your table and then use GET to pass the ID, then just call from the database on the next page using that ID.

Example: say Bob's ID is 1.

Code:
<a href="page.php?id=1">Bob</a>

Then onec you click that, do something like this:

Code:
$id = mysql_escape_string ($_GET['id']);

$sql = mysql_query ("SELECT * FROM table WHERE id='".$id."'");

PHP Sessions could work too but, personally I'd use the GET method if I could.
 
YES!!! i mean.... Yes, that is what i need...

but we're talking about more than 3 people. i worked it out, and my website would have roughly 180,000 entries lol. all in different tables of course. so the coding needs to be Completely dynamic. i dont want to actually have to type any data thats stored on the database. something where i can copy and paste the page for every stage down the structure of the site, and edit a little bit for each stage, but no actual typing of data if you get what i mean.

basically imagine that im doing some sort of census

the first page is "pick a surname"

then the second page is "pick a first name"

then the third page is "pick a town"

then the forth page is "pick a date"

can you give me some more insight into the GET command?
 
Yeah but that's using a form, you don't need to do that.

GET refers to the URL. You're getting bits of info from a URL. For example, if you see a site like this:

www.site.com/index.php?page=4&act=news&newsid=3

Or something like that, they are using GET to deal with the URL.

You turn each piece of info into a variable. Using the above example, you'd have something like this:

$page = $_GET['page'];
$page would equal 4

$act = $_GET['act'];
$act would equal news

$newsid = $_GET['newsid'];
$newsid would equal 3

This is completely dynamic by the way, you'll only be typing your database records.

I'll give you an example and you should be able to go from there.

Page.php -

PHP:
<?php

// First lets assign some variables
// I'm only going to assign userid for now
// I'll be using mysql_escape_string to escape the GET variables to prevent MySQL injection

$userid = mysql_escape_string ($_GET['userid']);

if (empty ($userid)) {
	
	// $userid is empty.... now we must display all of the names to click on
	
	$sql = mysql_query ("SELECT * FROM table ORDER BY user_id DESC");
	$num = mysql_num_rows ($sql);
	
	if ($num > 0) {
		// check if there are any users
		while ($row = mysql_fetch_array ($sql)) {
			echo '<a href="page.php?userid='.$row['user_id'].'">'.$row['username'].'</a><br />';
			// This is there we make the dynamic list of names
			// You just have to have a user_id in your table, and make it 
			// auto_increment so that each user gets his own ID - completely dynamic
		}
	} else {
		// no users
		echo 'There are no users in the database!';
	}
} else {
	// the $userid is not empty, so someone has clicked a name and been linked here
	// first lets check if the $userid is actually in the database
	
	$sql = mysql_query ("SELECT * FROM table WHERE user_id='".intval ($userid)."' LIMIT 1");
	// note the use of intval, this forces the string to be an integer - which also prevents MySQL injection
	$num = mysql_num_rows ($sql);
	
	if ($num > 0) {
		// the user IS in the database, so go ahead and display his information
		$row = mysql_fetch_array ($sql);
		
		echo '<a href="page2.php?userid='.$userid.'">'.$row['realname'].'</a>';
		// now this will link you to "page2.php"; there you can list the next set of information
	} else {
		// user was not in the database; return bad ID
		echo 'ERROR: Invalid ID!';
	}
}

?>

So using my example, on "page2.php" you could list the rest of his info. You could also do all of this on one page if you want, just keep checking the different variables if they are empty or not. If they aren't empty, just display the code for them, if they are empty, display the code for the previous variable.

If all variables are empty, just display the main page (which is what my code above will do).
 
Status
Not open for further replies.
Back
Top Bottom