Page 1 of 1

[Solved]-Making a dynamic link from a database object

Posted: Thu Apr 14, 2005 10:08 pm
by soianyc
Im trying to make a dynamic link from something i grabbed from a databse. The database shows an image and when i click on that image i want a link that gets created on a new tempelate with more pics included.

Here is the code i have written so far

Code: Select all

<body>
<br>
<br>
<?php

// Database Connection

include 'connect.php';

// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 6;


// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT * FROM coats LIMIT $from, $max_results");

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM coats"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
//echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    $prevs .=  "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<[Previous]</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
       //echo "$i ";
        } else {
         $pages .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\"> $i </a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    $nexts .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">[Next]>></a>";
}
//echo "</center>";
?>

<table class="mainTable" align="center">

<td align="left" valign="top" width="100">
		 <?coatmenu();?>
	</td>
<td align="center" valign="top" width="600">
<table class="topmiddle" >
<td class ="topcenterpic" colspan=2 align="center" width=350>Online Catalog<br><a class="pic" href="mensmain.php"><img src="pix/men.jpg"></a><img src="pix/dash.jpg"><img src="pix/women.jpg"><br><?//=$prevs?><?//=$pages?><?//=$nexts?></td><td width=250>Search:&nbsp; <INPUT maxLength=200 size=18></td>
<tr>
<?php
			$k=0;
				while($row = mysql_fetch_array($sql)){
				$l = 3;

				if (($k % $l) == 0){
				echo '<tr></tr>';
				}
				
				echo '<td class="maindisplay"><img src="pix/'.$row["name"].'"><br>'.$row["shortdescript"].'<br>'.$row["price"].'</td>';

				$k++;
				}
				?>
			
		</table>

</td>
</table>

</body>
Here is where i grab the data. i wanna make a link to a tempelate i have named mainitem.php

How do i proceed

Regards

Chris


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Apr 14, 2005 10:58 pm
by feyd
so..... make a link to the script.. what's the problem?

Posted: Fri Apr 15, 2005 8:42 am
by soianyc
Where im getting confused is, How does the link get the info for the page. For example. They click on a garment, which goes to a page that shows a detail description of that item. If i just make a link to it how does it know where to take the data from???

Posted: Fri Apr 15, 2005 8:46 am
by feyd
you pass information in the URL they are sent to.. something such as a record id to use for fetching the information from the database for display.

Posted: Fri Apr 15, 2005 9:51 am
by soianyc
So from what your saying, you create a link in the script, and you pass the row number into the new page????

Posted: Fri Apr 15, 2005 11:13 am
by soianyc
How do i setup a link to pass those records though??

Posted: Fri Apr 15, 2005 11:19 am
by feyd
you're already basically doing it in your while loop.. you just need to add the html code parts and the proper query string on the url.. :?

Posted: Fri Apr 15, 2005 11:20 am
by phpScott
if you look in the address bar while using this forum you see somethining like
viewtopic.php?t=32574&highlight=
and then when you click on the reply button you see something like
posting.php?mode=reply&t=32574

so in your <a href you migh have something like

Code: Select all

<a href=&quote;www.yoursite.com/detial.php?garmentId=23><img src=&quote;pic_23.gif&quote; /></a>
then on your detail page you would get the garmentId and do a db search to get the right data and display it.

phpScott

Posted: Fri Apr 15, 2005 1:48 pm
by soianyc
Ok, here is what i got, but it still isnt working.

Code: Select all

$k=0;
				while($row = mysql_fetch_array($sql)){
				$l = 3;

				if (($k % $l) == 0){
				echo '<tr></tr>';
				}
				$item_id = $row["id"];
				echo '<td class="maindisplay"><a href="mainitems.php?$item_id"><img src="pix/'.$row["name"].'"></a><br>'.$row["shortdescript"].'<br>'.$row["price"].'</td>';

				$k++;
				}
the link is not working, and i cant understand why. I think im on the right path .

[Solved]

Posted: Fri Apr 15, 2005 2:51 pm
by soianyc
Ok i got the rest of it to work.

Thanx to everyone for the help

-soianyc