Have code here for displaying data but ... need help ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
os2al
Forum Newbie
Posts: 14
Joined: Tue Jun 25, 2002 3:26 pm

Have code here for displaying data but ... need help ...

Post by os2al »

This bit of code displays member names in a <li> list </ul>, and when you click on a member name in the list, it shows you the data (name, address, city, state, zip) from a Mysql database at the bottom of the same list page; in other words, on the same page. Click on any name, you get the data info at the bottom of the same page, etc. I need it to display the data on another page or window instead of at the bottom of the <li> list page. Can anyone help me correct this code so it will do that?
Much appreciate any help. Thanks,

Al
- - -

<?php
function list_member()
{
$db_name = "database";
$table_name = "Table";

$connect = @mysql_connect("localhost", "database", "password");
if( !$connect )
{
echo "Couldn't connect to database";
exit;
}

$db = @mysql_select_db($db_name, $connect);
if( !$db )
{
echo "Couldn't select database";
exit;
}

$sql = "SELECT id, Name FROM $table_name";
$result = @mysql_query($sql,$connect);
while ($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
$Name = $row['Name'];
echo "<li><a href=\"$PHP_SELF?action=showmember&id=$id\">$Name</a>";
}
echo "</ul>";

mysql_close( $connect );
}
function member_details ( $id )
{
$db_name = "database";
$table_name = "table";

$connect = @mysql_connect("localhost", "database", "password");
if( !$connect )
{
echo "Couldn't connect to database";
exit;
}

$db = @mysql_select_db($db_name, $connect);
if( !$db )
{
echo "Couldn't select database";
exit;
}

$sql = "SELECT * FROM $table_name WHERE id='$id'";
$result = @mysql_query($sql,$connect);
while ($row = mysql_fetch_assoc($result))
{
$Name = $row['Name'];
$Address = $row['Address'];
$City = $row['City'];
$State = $row['State'];
$Zip = $row['Zip'];
}
echo"

<h3>$Name</h3>

<strong>Address: </strong>$Address <br>
<strong>City: </strong>$City<br>
<strong>State: </strong>$State<br>
<strong>Zip: </strong>$Zip<br>

mysql_free_result($result);
mysql_close ($connect);
}
?>
<html>
<head>
<title>Members List</title>

</head>

<?php
list_member();
switch( $action )
{
case "showmember":
member_details( $id );
break;
default:
echo "Some instructions here";
break;
}
?>
<h4><strong><br> Choose another member<br></strong></a></h4>
<br>
</body>
</html> :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

in function list_member() change

Code: Select all

echo "&lt;li&gt;&lt;a href="$PHP_SELF?action=showmember&amp;id=$id"&gt;$Name&lt;/a&gt;";
to

Code: Select all

echo "&lt;li&gt;&lt;a href="$PHP_SELF?action=showmember&amp;id=$id" target="memberDetail"&gt;$Name&lt;/a&gt;";
and change

Code: Select all

&lt;?php
list_member(); 
switch( $action ) 
{ 
case "showmember": 
member_details( $id ); 
break; 
default: 
echo "Some instructions here"; 
break; 
}
?&gt;
to

Code: Select all

&lt;?php 
if (!isset($action))
	list_member(); 
else
{
	switch( $action ) 
	{ 
		case "showmember": 
			member_details( $id ); 
			break; 
		default: 
			echo "Some instructions here"; 
			break; 
	} 
}
?&gt;
and read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+ ;)
os2al
Forum Newbie
Posts: 14
Joined: Tue Jun 25, 2002 3:26 pm

Many thanks, Volka ...

Post by os2al »

:D Followed your instructions and they worked perfectly, so I am very grateful for your help. You truly are a master and could teach Yoda a few tricks.

I'm ashamed to ask for anything else, but it would be that the data opens in a way that would allow me to use the browser's back button to go back and forth between the list and the record info. As it stands now (and they are perfectly good, believe me) when one clicks on the list name, you go to another window to see the record data after the first record appears and must keep switching windows. I hoped it would just be another page that I could move back and forth around, using the back button; click on a name, see the data, and go back again. But I couldn't explain it well enough.

I much appreciate your help. You've been good to me.

Al
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

So do you want the user detail to open in a new window or the same window?
Your English is too good for me :lol:
os2al
Forum Newbie
Posts: 14
Joined: Tue Jun 25, 2002 3:26 pm

Sorry about the English ...

Post by os2al »

:? As I tried to explain but didn't well enough, so that the data appears not on the same page as the list but in a way that let's me move back and forth to the list using the brower's Back button -- on a new web page is perhaps I'm trying to say. Just as, say, when you login to a site, you could go back to the login screen, even though you're on a different webpage after the login.
Sorry about this. Thanks for bothering with my confusion. How exactly to explain it other than that is unclear to me (and obviously to anyone else).

Al
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Re: Sorry about the English ...

Post by Takuma »

os2al wrote::? As I tried to explain but didn't well enough, so that the data appears not on the same page as the list but in a way that let's me move back and forth to the list using the brower's Back button
Ah, do you mean by this?

Code: Select all

&lt;?php
echo "&lt;li&gt;&lt;a href="$PHP_SELF?action=showmember&amp;id=$id" target="_blank"&gt;$Name&lt;/a&gt;";
?&gt;
os2al
Forum Newbie
Posts: 14
Joined: Tue Jun 25, 2002 3:26 pm

Well, it's the same ...

Post by os2al »

8O Sorry to say, despite your heroic efforts, that the code does the same thing, from what I can see. Much appreciate your effort, though.

Al
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

So the data retrieval is working but not the window?
os2al
Forum Newbie
Posts: 14
Joined: Tue Jun 25, 2002 3:26 pm

Works as before the change

Post by os2al »

:o Well, it works just as it did before. The window opens and the data shows, the only "problem" (if this is a problem) is that it's a different window but one that doesn't allow me to use the browser's back button to go back to the list. To get to the list again, I have to switch windows, click on another name, and then switch back to see the data in the other window. I hope this makes sense.

Al
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Would this work?

Code: Select all

&lt;?php
echo "&lt;li&gt;&lt;a href="$PHP_SELF?action=showmember&amp;id=$id"&gt;$Name&lt;/a&gt;";
?&gt;
Post Reply