Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic display on same page

Status
Not open for further replies.

gillrowley

Programmer
Apr 2, 2004
59
US
Hello - need help figuring out what's wrong. I'm new to PHP. I'm trying to display info in one <div> section that is dependent on which hyperlink is clicked in another <div> section on the same page. The table and hyperlinks on the left side display properly, but I can't get anything to display on the right hand side. Help, please? Here's the code chunk.

<!-- left hand side - no problems except onclick doesnt work -->
<div class="content_half_left">
<?php include("includes/config.php"); ?>
<?php include("includes/opendb.php"); ?>
<?php $get_dogs_sql = "SELECT *, stateName FROM availabledogs ";
$get_dogs_sql .= "INNER JOIN states on state = stateCode ";
$get_dogs_sql .= "ORDER BY state, dogName";
$get_dogs_res = mysql_query($get_dogs_sql);
if (mysql_num_rows($get_dogs_res) > 0) {
$display = "<table cellpadding=1 cellspacing=0 border=0 width=\"100%\">";
$display .= "<tr><th>Dog</th><th>State</th><th>Rescue Contact</th></tr>";
while ($get_info = mysql_fetch_array($get_dogs_res)) {
$dog_id = $get_info['dogID'];
global $dog_id;
$dogName = $get_info['dogName'];
$state = $get_info['stateName'];
$rcontact = $get_info['rescueContact'];
$display .= "<tr>";
$display .= "<td><a href=\"#\" onclick=\"displayDogs(".$dog_id.")\">".$dogName."</a></td>";
$display .= "<td>".$state."</td><td>".$rcontact."</td>";
$display .= "</tr>";
}
$display .= "</table>";
} else {
$display = "<p>Sorry, there are no dogs available at this time.</p>";
}
mysql_free_result($get_dogs_res);
echo $display;
?>
<?php include("includes/closedb.php"); ?>
</div>
<!-- right hand side - nothing displays here -->
<div class="content_half_right">
<p>Click on a dog's name to see the bio</p>
<?php function displayDogs($dog_id) {
$dog_info_query = "SELECT * FROM availabledogs WHERE dogID = ".$dog_id;
$dog_info_res = mysql_query($dog_info_query);
$get_dog_info = mysql_fetch_array($dog_info_res);
$dogName = $get_dog_info['dogName'];
$contact = $get_dog_info['rescueContact'];
$gender = $get_dog_info['gender'];
$result = "<h3>".$dogName."</h3>";
$result .= "<p>Rescue Contact: ".$contact."</p>";
$result .= "<p>Gender: ".$gender."</p>";
echo $result;
}
?>
</div>
 
I understand your problem. It's probably here:

$display .= "<td><a href=\"#\" onclick=\"displayDogs(".$dog_id.")\">".$dogName."</a></td>";


You're treating PHP functions like you would JavaScript functions. You can use JavaScript in an onclick only because JavaScript and HTML both are used on the client side, within the browser. PHP runs on the server and there is no callable connection between what is going on in the browser and PHP code that resides on the server.

PHP web scripts produce dynamic output because data is submitted to them through an HTML connection. This can be done through submitting a form, by clicking on a link that has data embedded in the URL, or by a technique like AJAX.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks - I learned something. On to figure out plan B.
 
I figured it out - here's the updated code -
<?php
$get_dogs_sql = "SELECT *, stateName FROM availabledogs ";
$get_dogs_sql .= "INNER JOIN states on state = stateCode ";
$get_dogs_sql .= "ORDER BY state, dogName";
$get_dogs_res = mysql_query($get_dogs_sql);
if (mysql_num_rows($get_dogs_res) > 0) {
$display = "<table cellpadding=1 cellspacing=0 border=0 width=\"100%\">";
$display .= "<tr><th>Dog</th><th>State</th><th>Rescue Contact</th></tr>";
while ($get_info = mysql_fetch_array($get_dogs_res)) {
$dog_id = $get_info['dogID'];
global $dog_id;
$dogName = $get_info['dogName'];
$state = $get_info['stateName'];
$rcontact = $get_info['rescueContact'];
$display .= "<tr>";
$display .= "<td><a href=\"".$_SERVER['PHP_SELF']."?dog_id=".$dog_id."\">".$dogName."</a></td>";
$display .= "<td>".$state."</td><td>".$rcontact."</td>";
$display .= "</tr>";
}
$display .= "</table>";
} else {
$display = "<p>Sorry, there are no dogs available at this time.</p>";
mysql_free_result($get_dogs_res);
echo $display;

echo "</div>";
echo "<div class=\"content_half_right\">";
if (isset($_GET["dog_id"])) {
$dog_info_query = "SELECT * FROM availabledogs WHERE dogID = ".$_GET["dog_id"];
$dog_info_res = mysql_query($dog_info_query);
$get_dog_info = mysql_fetch_array($dog_info_res);
$dogName = $get_dog_info['dogName'];
$contact = $get_dog_info['rescueContact'];
$gender = $get_dog_info['gender'];
$result = "<h3>".$dogName."</h3>";
$result .= "<p>Rescue Contact: ".$contact."</p>";
$result .= "<p>Gender: ".$gender."</p>";
echo $result;
}
?>
</div>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top