 |
 |
 |
| |
Help with PHP Dynamic Hyperlinking |
View full version |
|
User #225662 21 posts
Participant
|
Hi, I have created a website for a bookshop and have made a catalogue page that allows the user to choose what they want to search for from a drop down box. the page then links to a mysql DB and returns the chosen selection. this all works perfectly.. the problem is after the general section is made i want the user to be able to then click on any particular book and be shown a more detailed product description (that has like a image and more details..etc) all this stuff is called from the SQL DB which i have made.. but i want to dynamicly link it, not just create an individual page for each book which would take me forever to do... HHHEEEEELLLLPPPPP ive got no idea how to do it. My code for the selection page is attached below.
================================== =================== <?php //This connects to the Database & shows error if it cant connect $con = mysql_connect("####","####","####"); if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("cfb", $con);
echo "<center><table border=0 cellpadding=0 cellspacing=0>";
$selected_category = $_REQUEST selection; $result = mysql_query("SELECT * FROM books WHERE sub = '$selected_category' ");
$page = $_REQUESTpage;
$numRows = mysql_num_rows($result);
$NUM_RECS_PP = 4;
$totalNumPages = ceil($numRows / $NUM_RECS_PP) - 1;
$numPreviousPages = $page - 1;
$numNextPages = $totalNumPages - $numPreviousPages;
for ($i = 0; $i < ($page -1) * $NUM_RECS_PP; $i++) { $row = mysql_fetch_array($result); //discard }
$line = 0;
echo "<tr>"; echo "<td width='150'<br/><br/>I sbn</p></td>"; echo "<td width='100'<br/><br/>T itle</p></td>"; echo "<td width='100'<br/><br/>A uthors</p></td>"; echo "<td width='100'<br/><br/>P ublisher</p></td>"; echo "<td width='100'<br/><br/>P rice</p></td>"; echo "</tr>";
while(($row = mysql_fetch_array($result)) && ($line < $NUM_RECS_PP) ) {
echo "<tr>"; echo "<td width='150'><br /><br/><br/>" . $rowisbn. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowtitle. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowauthors. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowpublisher. " </p>"; echo "<td> <br /> <br/><br/> $" .$rowprice ." </p>"; echo "<br/><br/>" . $rowitemNo ."</p><br/><br/>< /p></td>";
echo "<tr valign=bottom>"; echo "<td bgcolor=#ffffff background='img/strichel.gif' colspan=3><img src=img/blank.gif width=1 height=50></td>"; echo "</tr></tr>"; $line++; }
echo "<tr><td align='center' colspan=3><br/><br/> </p><br/><br/>< /p> ";
for ($i = 0; $i < $numPreviousPages; $i++) { $pnum = $i+1; $pnumDisplayed = $pnum; echo "<a href=FCBCatalogue_Selection.php?pa ge=$pnum&selection=$selected_categ ory>$pnumDisplayed "; }
echo $page ." "; for ($i = 0; $i < $numNextPages; $i++) { $pnum = $i+$numPreviousPages +2; $pnumDisplayed = $pnum; echo "<a href=FCBCatalogue_Selection.php?pa ge=$pnum&selection=$selected_categ ory>$pnumDisplayed "; }
echo "</td></tr>"; echo "</table></center>"; mysql_close($con);?> ================================== =============
|
posted 2008-May-6, 11am AEST
edited 2008-May-6, 12pm AEST
|
|
User #27610 473 posts
Forum Regular
|
in your mysql fetch loop, you could add a link to the title like this (i assume itemNo is your unique id for each book in the table)
echo "<td width='100'><a href=\"yourbookdetailspage.php?id= \"" . $row["itemNo"] . "\"> . $row["title"] . "</a></td>";
You'd then create your page that you want to display the book details. You can retrieve the id by using: $id = $_GET["id"];
then just query the database using that id, then display your book details as required.
You should clean up a lot of that html, your table row's - <td> - should be closed with </td> not with </p>. Also, instead of using line breaks - <br/> - consider using CSS to pad out the text
|
posted 2008-May-6, 12pm AEST
edited 2008-May-6, 12pm AEST
|
|
User #64825 346 posts
Forum Regular
|
thesimpsons666 writes... $con = mysql_connect(###, ###,###);
DUDE! You really might want to edit that post and remove the details from the database connection line!
|
posted 2008-May-6, 12pm AEST
|
|
User #225662 21 posts
Participant
|
opps... thanks heaps man..
|
posted 2008-May-6, 12pm AEST
|
|
User #225662 21 posts
Participant
|
thanks for the reply, i put that line in however i get an Parse error: Parse error when i try to run it
echo "<td width='150'><br /><br/><br/>" . $rowisbn. " </p>"; echo "<td width='100'><a href=\"FCBCatalogue_Indiv_Selectio n.php?id= \"" . $rowitemNo . "\"> . $rowtitle . "</td>"; echo "<td width='100'><br /><br/><br/>" . $rowtitle. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowauthors. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowpublisher. " </p>"; echo "<td> <br /> <br/><br/> $" .$rowprice ." </p>"; echo "<br/><br/>" . $rowitemNo ."</p><br/><br/>< /p></td>";
Is there something wrong with my syntax? The \"" and "\"> you use are new to me.. what do they actually do? (sorry i am a bit of a newb with php)
|
posted 2008-May-6, 12pm AEST
|
|
User #225662 21 posts
Participant
|
sorry the echo "<td width='100'><br /><br/><br/>" . $rowtitle. " </p>"; should be incased in /*....*/ (but it still doesnt work) :)
|
posted 2008-May-6, 12pm AEST
|
|
User #225662 21 posts
Participant
|
sorry the echo "<td width='100'><br /><br/><br/>" . $rowtitle. " </p>"; should be removed (but it still doesnt work) :)
|
posted 2008-May-6, 12pm AEST
|
|
User #214952 68 posts
Participant
|
thesimpsons666 writes... <a href=\"FCBCatalogue_Indiv_Selectio n.php?id= \"" . $rowitemNo . "\">
I'm no PHP guru but isnt it <a href=\"FCBCatalogue_Indiv_Selectio n.php?id= " . $rowitemNo . "\"> .......
|
posted 2008-May-6, 1pm AEST
|
|
User #225662 21 posts
Participant
|
thanks for reply. it is however this doesnt work ether... what do the \ do? ive not used them before to hyperlink stuff?
|
posted 2008-May-6, 1pm AEST
|
|
User #27610 473 posts
Forum Regular
|
Xception writes... I'm no PHP guru but isnt it <a href=\"FCBCatalogue_Indiv_Selectio n.php?id= " . $rowitemNo . "\"> .......
Oops yep! Unnecessary quotation there..
\ is the general escape character. It lets you insert a double quotation mark into a string (being already enclosed by quotes) as well as other special characters that can't be represented textually, like a new line (\n) etc.
How isnt it working with the change? Any particular error?
|
posted 2008-May-6, 1pm AEST
edited 2008-May-6, 1pm AEST
|
|
User #225662 21 posts
Participant
|
hey thanks for the reply, um i am getting an Parse error: and it says it was expecting T_String...
echo "<td width='150'><br/>" . $rowisbn. " </p>"; echo "<td width='100'><a href=\"FCBCatalogue_Indiv_Selectio n.php?id= "\ . $rowitemNo . "\"> . $rowtitle . "</td>"; echo "<td width='100'><br/>" . $rowtitle. " </p></td>"; echo "<td width='100'><br/>" . $rowauthors. " </p></td>"; echo "<td width='100'><br/>" . $rowpublisher. " </p></td>"; echo "<td width='100'><br/>" . $rowpublisher. " </p></td>"; echo "<td width='100'><br /> $" .$rowprice ." </p></td>";
not sure if its a syntax error... do i have to declare id before i use it here?
|
posted 2008-May-6, 4pm AEST
|
|
User #15538 10086 posts
Whirlpool Forums Addict
|
If you have to put both HTML and PHP in the same file, you're much better off escaping out of PHP back into HTML rather than echoing all of your HTML.
Your code is horrendous! You'll find it much easier to follow if you clean it up.
For example instead of this:
$line = 0;
echo "<tr>"; echo "<td width='150'<br/><br/>I sbn</p></td>"; echo "<td width='100'<br/><br/>T itle</p></td>"; echo "<td width='100'<br/><br/>A uthors</p></td>"; echo "<td width='100'<br/><br/>P ublisher</p></td>"; echo "<td width='100'<br/><br/>P rice</p></td>"; echo "</tr>";
while(($row = mysql_fetch_array($result)) && ($line < $NUM_RECS_PP) ) {
echo "<tr>"; echo "<td width='150'><br /><br/><br/>" . $rowisbn. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowtitle. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowauthors. " </p>"; echo "<td width='100'><br /><br/><br/>" . $rowpublisher. " </p>"; echo "<td> <br /> <br/><br/> $" .$rowprice ." </p>"; echo "<br/><br/>" . $rowitemNo ."</p><br/><br/> </p></td>";
echo "<tr valign=bottom>"; echo "<td bgcolor=#ffffff background='img/strichel.gif' colspan=3><img src=img/blank.gif width=1 height=50></td>"; echo "</tr></tr>"; $line++; }
Do this:
<tr> <td width='150'<br/><br/>I sbn</p></td> <td width='100'<br/><br/>T itle</p></td> <td width='100'<br/><br/>A uthors</p></td> <td width='100'<br/><br/>P ublisher</p></td> <td width='100'<br/><br/>P rice</p></td> </tr>
<?php $line = 0; while (($row = mysql_fetch_array($result)) && ($line < $NUM_RECS_PP) ) { ?>
<tr> <td width='150'><br /><br/><br/><?ph p echo $rowisbn ?></p> <td width='100'><br /><br/><br/><?ph p echo $rowtitle ?></p> <td width='100'><br /><br/><br/><?ph p echo $rowauthors ?></p> <td width='100'><br /><br/><br/><?ph p echo $rowpublisher ?></p> <td> <br /> <br/><br/> $<?php echo $rowprice ?></p> <br/><br/><?php echo $rowitemNo ?></p><br/><br/> </p></td>
<tr valign=bottom> <td bgcolor=#ffffff background='img/strichel.gif' colspan=3><img src=img/blank.gif width=1 height=50></td> </tr></tr> <?php $line++; } ?>
I would normally go much further than this, but in the interest of keeping it easy to understand basically just don't ever echo HTML unless you really really think you need to.
|
posted 2008-May-6, 5pm AEST
|
|
User #27610 473 posts
Forum Regular
|
thesimpsons666 writes... hey thanks for the reply, um i am getting an Parse error: and it says it was expecting T_String...
echo "<td width='150'><br/>" . $rowisbn. " </p>"; echo "<td width='100'><a href=\"FCBCatalogue_Indiv_Selectio n.php?id= "\ . $rowitemNo . "\"> . $rowtitle . "</td>";
Need to remove the backslash after the id="
|
posted 2008-May-6, 5pm AEST
edited 2008-May-6, 10pm AEST
|
|
User #225662 21 posts
Participant
|
thanks heaps man... didnt know you could do that, will change it
|
posted 2008-May-6, 5pm AEST
|
|
User #225662 21 posts
Participant
|
to khorne... thanks man il try it when i get near a computer that has xampp n get back to u
|
posted 2008-May-6, 5pm AEST
|
|
User #225662 21 posts
Participant
|
hey man . i removed the backlash so it looks like this: echo "<td width='100'><a href=\"FCBCatalogue_Indiv_Selectio n.php?id=".$rowitemNo ."\">.$rowtitle. </td>";
but i am still getting this error.
Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\xampp\htdocs\info316\S000479 02\week8\assignment\FCBCatalogue_S election.php on line 90
|
posted 2008-May-7, 12pm AEST
|
|
User #225662 21 posts
Participant
|
khorne, i worked it out (it was so silly), <td width='100'> <a href=\"FCBCatalogue_Indiv_Selectio n.php?id=".$rowitemNo."\> ".$rowtitle. "</td>"; just needed to move the " mark .. thanks heaps for your help man your a life saver
|
posted 2008-May-7, 12pm AEST
|
|
User #156046 225 posts
Forum Regular
|
Mark had good advice. You really need to clean this up, it will be much easier to work with.
It's also worth pointing out that your HTML is completely hidously broken, and you'd see that clearly if you formatted it right (though I'm surprised Mark didn't comment on it).
The HTML here is bad enough that it simply won't work. Be very careful with HTML in PHP. It's very easy to make a small mistake 1000 times. (And no, I couldn't be bothered fixing the whirlcoded variables.)
<?php $line = 0; while (($row = mysql_fetch_array($result)) && ($line < $NUM_RECS_PP) ) { ?>
<tr> <td><?phÂp echo $rowisbn ?></td> <td><?php echo $rowtitle ?></td> <td><?php echo $rowauthors ?></td> <td><?php echo $rowpublisher ?></td> <td>$<?php echo $rowprice ?></td> <td><?php echo $rowitemNo ?></td> </tr> <tr> <td bgcolor=#ffffff background='img/strichel.gif' colspan=6><img src=img/blank.gif width=1 height=50></td> </tr> <?php $line++; } ?>
|
posted 2008-May-8, 2pm AEST
|
|
User #15538 10086 posts
Whirlpool Forums Addict
|
Matt AustralianGamer writes... though I'm surprised Mark didn't comment on it
I have a decent in built 'noise filter' these days.
Looking at that code was like staring at fresh vomit. The first thing I needed to point out was to regulate your intake of carrots. After that I became nauseous and could not comment on the spaghetti :)
|
posted 2008-May-8, 3pm AEST
|
|
User #225662 21 posts
Participant
|
to Marks... thanks both for your advice i will clean that code up so it reduces vomitting symtoms
|
posted 2008-May-9, 11am AEST
|
|
User #15538 10086 posts
Whirlpool Forums Addict
|
Haha no worries. If you need any help with the best ways to clean up let me know.
|
posted 2008-May-9, 2pm AEST
|
|
User #225662 21 posts
Participant
|
Hey I do have a question about java if you know anything about it. I am trying to validate a page using Strict DTD but it doesnt like the line
else if (username.indexOf ("@")<0) {
It says character "<" is the first character of a delimiter but occurred as data.
i know i could use Transitional but i would like to understand why it doesnt like this
|
posted 2008-May-12, 12pm AEST
|
|
User #65488 1072 posts
Whirlpool Enthusiast
|
thesimpsons666 writes... java
I think you mean JavaScript
Strict DTD
I assume you mean XHTML STRICT in which case, you need to wrap your JavaScript within CDATA tags, ie
<script type="text/javascript"> //<![CDATA[ function someFunc() { ... .. . //]]> </script>
Another solution would be to place your JavaScript into a separate JS file, ie
<script type="text/javascript" src="myscript.js"></script>
Edit: See developer.mozilla.org/en...tead_of_Comments My preference is still to use external files.
|
posted 2008-May-12, 12pm AEST
edited 2008-May-12, 1pm AEST
|
|
User #225662 21 posts
Participant
|
thanks heaps.. yeah externally placing them works and looks abit cleaner.. cheers !!
|
posted 2008-May-12, 1pm AEST
|