Jump to content

[SOLVED] a link that updates dynamic data


joshlindem

Recommended Posts

I'm not exactly sure what this is called, so it's hard to do a search on it.

 

What I'm trying to do is have a webpage that updates the dynamic data from a database based on what links are chosen on the left side of the page.

 

- On one side of the page is a list of links. (ie: potato, carrot, apple...)

 

- On the other side of the page is a bunch of dynamic data that connects to a MySQL database.

 

Depending on what is chosen on the left side of the page, it will show the user that information from the database on the right side.

Example:

If you click on the apple link, it pulls the apple row out of the database and presents a picture of an apple, gives some information on an apple, etc.

 

I don't think this is very hard but I don't see it in any of the books I have, and have never done it before. Can someone help point me in the right direction or tell me how this is done?

 

BTW... I'm using Dreamweaver 8 and use PHP/MySQL

 

Thanks,

Josh

Link to comment
Share on other sites

ok, that makes sense to what I was thinking.  That some how when you clicked on an item in the list, that other side updates based on whatever the query string says should be displayed.

 

Now.....  how do I do this?  I understand what it is you're saying.  The question I have is 'how do I do it?'  I'm a visual learner so I need to see it in practice first to break it down and figure out how to modify it to what I'm doing.

 

Can you show me how to do this?  Is there a link you can send me with a tutorial?  Or if you like I can really break down what it is I'm doing and let you try and explain from there.

 

or anyone else that wants to jump in.

 

Thanks!

Link to comment
Share on other sites

I'm going to assume you have a database already setup and just talk about the PHP. If you haven't just say.

 

<?php

// Firstly you need to connect to your database. If it fails, MySQL will generate an error for you.
$conn = mysql_connect("http://example.com", "username", "password") or die(mysql_error());

//Then you need to select your database.
mysql_select_db('DATABASE_NAME_HERE', $conn) or die(mysql_error());

//Here are your links that would be on the left. I'll leave the HTML that positions them to the left up to you.
echo '<a href="http://example.com/index.php?data=apple">Apple</a>';
echo '<a href="http://example.com/index.php?data=potato">Potato</a>';
echo '<a href="http://example.com/index.php?data=carrot">Carrot</a>';

//The ?data= part is passing a variable via the URL. The variable value is that of the link you've clicked.

//Now for determining what to display, based on what the data variable's value is. The variable is stored in the GET global.

if ($_GET['data'] == "apple") {
     //Display what you want to display for an Apple.
}
if ($_GET['data'] == "potato") {
     //Display what you want to display for a Potato.
}
if ($_GET['data'] == "carrot") {
     //Display what you want to display for a Carrot.
}

?>

 

Hope that helps.

 

Link to comment
Share on other sites

<?php

// Connecting To Database
mysql_connect("localhost",'username","password");

// Selects Database You Want To Connect To
mysql_select_db("MyDatabase");

// Querys Database
$pick = $_GET['pick'];
$results = mysql_query("select * from MyDataTableName where pick='$pick'");

// Pulls Data From Database & Display It
while ($r=mysql_fetch_array($results)) {

$fruits=$r["fruits"];

echo "You Picked $fruit";

}

?>

Link to comment
Share on other sites

To answer a one major question, yes I have my website connected to a database. And yes I have pulled from it before.  But normally when I do things like that it's been simple things such as a random quote generator type application or a repeat region spanning over a couple pages presenting items.

 

Maybe I should have nixed my apple\potato\carrots example since I think back and figure that was a stupid example and it's confusing me a little.

 

What I'm doing is working on a small Alumni website, and this one page is a little confusing since I haven't done anything like it before. 

The idea is that one side of the book lists everyone's name.  The other side of the book has the dynamic data links that will show things like persons picture, name, location, email, family, occupation, etc, etc, etc.  And this will change depending on the persons name they click on.

 

I think I understand that I need to have a persons name a link with $autonum (where the number corrosponds to that row of their info in the database)

But on the other side of the page where the information is to be displayed.... I guess where I'm really getting lost is how to pass that information to the other side.

There will be approximately 50 people listed, and I'm afraid of having do a 50 long IF Statement.

 

So on name side I would have people listed like this?

domain.com/page.php?person=1
domain.com/page.php?person=2
domain.com/page.php?person=3
domain.com/page.php?person=4

Now on the other side I would have to create a RecordSet to have something similar to this:

$person = $_GET['person'];
$query_Recordset1 = "SELECT picture, name, email, occupation FROM people_database WHERE person='$person'");

 

Sorry I'm at work right now and can't test it out, so I'm trying to make sure I get it right before I go home.

 

Link to comment
Share on other sites

 

ok, so playing around with it tonight I finally have it figured out.  Apparently I found after testing it out that you cannot use the same name as the field you are ultimately trying to call.

What I mean is if your table column is called auto.... don't call the parameter auto throughout.  It just doesn't like you very much if you do.

 

I thought I would post my code for those that are interested.

 

The upper code before the <head> starts

<?
mysql_select_db($database_test, $test);
$something = $_GET['num'];
$query_Recordset1 = "SELECT auto, image, `desc`, `comment` FROM test WHERE auto='$something'";
$Recordset1 = mysql_query($query_Recordset1, $test) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

 

The links that call the specific row in the table

<p><a href="test.php?num=1">Set 1</a></p>
<p><a href="test.php?num=2">Set 2</a></p>
<p><a href="test.php?num=3">Set 3</a></p>
<p><a href="test.php?num=4">Set 4</a></p>

 

And finally the code that displays the 3 items based upon the 'auto' field

<p><img src="<?php echo $row_Recordset1['image']; ?>" /></p>
<p> <?php echo $row_Recordset1['desc']; ?></p>
<p> <?php echo $row_Recordset1['comment']; ?></p>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.