 |
| Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 3rd, 2004, 09:36 PM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I need some help on this php problem.
Hi. I'm Major. I hope someone here can help me out with this problem. I am developing a shopping cart for my bookstore. I am trying to pass some variables(isbn, title, price) from bookdetails.php to showcart.php.
The problem is when I pass the variable(from bookdetails.php) and use the $_GET function(at showcart.php), it shows me the variable name, and not the variable value. I have tried various ways to get the variable to the next page; showcart.php, but failed.
I hope someone in this forum can help me out with this problem.
Thanks.
Regards,
Major
Below is the source code for bookdetails.php
=============================================
<html>
<head>
<title>Book Detail</title>
<link rel="stylesheet" href="fontlib.css" style="text/css">
</head>
<body>
<style>
<!--
A {text-decoration: none }
-->
</style>
<body background="#ffffff" topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<table border=0 width=100% height=10 cellspacing=0 cellpadding=0 bgcolor=#8FBC8F>
<tr><td>
<img src="./picturefiles/company_logo.jpg" border=1 align=center> <b>ikhtiar-resources</b>.com<br/>
</td>
</tr>
</table>
<table border=0 width=100% height=10 cellspacing=0 cellpadding=0 bgcolor=black>
<tr><td align=right>
<img src="./picturefiles/bluebullet.gif" align=center> <a href="index.php"><b class="white1">Home</b></a>
<img src="./picturefiles/bluebullet.gif" align=center> <a href="books.php"><b class="white1">Books</b></a>
<img src="./picturefiles/bluebullet.gif" align=center> <a href="faq.php"><b class="white1">FAQ</b></a>
<img src="./picturefiles/bluebullet.gif" align=center> <a href="help.php"><b class="white1">Help</b></a>
<img src="./picturefiles/bluebullet.gif" align=center> <a href="emailus.php"><b class="white1">Email Us</b></a>
<img src="./picturefiles/bluebullet.gif" align=center> <a href="search.php"><b class="white1">Search</b>
</td>
</tr>
</table>
[list]<img src="./picturefiles/previousbluearrow.gif" align=center><a href="javascript:history.back()"><b class="black1">[u]Previous Page</u></b></a>
|
<a href="javascript:history.forward()"><b class="black1">[u]Next Page</u></b></a><img src="./picturefiles/nextbluearrow.gif" align=center></ul>
<br>
<?php
echo '[list]';
$bookdetail = $HTTP_GET_VARS['id'];
include("./exc/psal.inc");
$query_bookdetail = "select
b_covers.isbn_cover, b_covers.price,
b_covers.fk_cover_id,
b_cats.isbn_category, b_cats.title,
bs.book_isbn, bs.book_author,
bs.book_description, bs.book_url,
bs.book_misc, bs.book_pages,
bs.fk_publisher_id, c.cover, mp.mth_id,
mp.mth, yp.yr_id, yp.yr, p.publisher_id,
p.publisher_name
from books bs, books_categories b_cats,
books_covers b_covers, covers c,
mth_published mp, yr_published yp,
publishers p
where bs.book_isbn='$bookdetail' and
b_covers.isbn_cover = '$bookdetail' and
b_cats.isbn_category = '$bookdetail' and
c.cover_id = b_covers.fk_cover_id and
bs.fk_mth_id = mp.mth_id and
bs.fk_yr_id = yp.yr_id and
bs.fk_publisher_id = p.publisher_id";
$result_bookdetail = mysql_query($query_bookdetail) or die("Error in query");
$num_result_bookdetail = mysql_num_rows($result_bookdetail);
if (mysql_num_rows($result_bookdetail) > 0) {
while($row = mysql_fetch_object($result_bookdetail)) { ?>
<? echo '<table border=0 bgcolor=#ffffff width=80%' ?>
<? echo '<tr>' ?>
<? echo '<td colspan=2><b class="black6">'
.$row->title. '</b><br><br></td>'; ?>
<? echo '</tr>' ?>
<? echo '<tr>' ?>
<? echo '<td>' ?>
<img src="<? echo $row->book_url; ?>" border=1>
<? echo '</td>' ?>
<? echo '<td valign=top>' ?>
<? echo '<b>ISBN: </b>'
.$row->book_isbn. ''?>
<? echo '<br>' ?>
<? echo '<b>Author:
</b>' .$row->book_author. '' ?>
<? echo '<br>' ?>
<? echo '<b>Pages:
</b>' .$row->book_pages. ' pages.' ?>
<? echo '<br>' ?>
<? echo '<b>Cover:
</b>' .$row->cover. '' ?>
<? echo '<br>' ?>
<? echo '<b>Published by:
</b>' .$row->publisher_name. '' ?>
<? echo '<br>' ?>
<? echo '<b>Date Published:
</b>' .$row->mth. ', ' ?>
<? echo ''
.$row->yr. '' ?>
<? echo '<br>' ?>
<? echo '<b>Price(RM):
</b>' .$row->price. '' ?>
<? echo '<i>(Price is subject
to change without prior notice)</i>'?>
<? echo '<br>' ?>
// Here's where the problem is. I am trying to pass the variable's
// value from $row->book_isbn to $id.
<? $isbn = $row->book_isbn; $id=$isbn; ?>
<? echo $isbn; ?>
<? echo '<a href="showcart.php?id=
<? echo $id ?>">Add to cart</a>'; ?>
<? echo '</td>' ?>
<? echo '</tr>' ?>
<? echo '</table>' ?>
<? echo '<br>' ?>
<? echo '<table border=0 bgcolor=#ffffff width=80%>' ?>
<? echo '<tr>' ?>
<? echo '<td>' ?>
<? echo '' ?>
<? echo '
<b class="black6">[u]Description</u></b><br><br>' ?>
<? echo ''
.nl2br($row->book_description). '' ?>
<? echo '</td>' ?>
<? echo '</tr>' ?>
<? echo '</table>' ?>
<? }
}
mysql_close($connection);
echo '</ul>';
?>
</body>
</html>
Below is the source code for showcart.php
=========================================
<html>
<head><title>showcart</title></head>
<body>
// Inside here, no value is printed out. Instead it prints $id on
// the screen.
<?php
$isbn = $HTTP_GET_VARS['id'];
echo $isbn;
?>
</body>
</html>
|
|

March 3rd, 2004, 09:58 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
I don't know if this will help you or not, but HTTP_*_VARS is no longer in use (unless you're using a very old version, which you really shouldn't be :))
Use $_GET[]
Gotta go,
----------
---Snib---
----------
|
|

March 3rd, 2004, 10:37 PM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No, the $_GET method didn't work for me. I tried that before this.
regards,
major
|
|

March 4th, 2004, 06:05 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I didn't see anywhere in your code where you actually SET id in a URL parameter. You try to set it here:
Quote:
quote:
<? $isbn = $row->book_isbn; $id=$isbn; ?>
<? echo $isbn; ?>
<? echo '<a href="showcart.php?id=
<? echo $id ?>">Add to cart</a>'; ?>
<? echo '</td>' ?>
|
But that won't work. You open a single quoted string, and echo everything up to the closing single quote mark.
Try this:
echo "<a href=\"showcart.php?id={$id}\">Add to cart</a>";
See the difference? Your version would also work if you terminated your single-quoted string as follows:
<? $isbn = $row->book_isbn; $id=$isbn; ?>
<? echo $isbn; ?>
<? echo '<a href="showcart.php?id='; ?>
<? echo $id ?>
<? echo '">Add to cart</a>'; ?>
<? echo '</td>' ?>
I can't help but mention that your code is kinda sloppy... you enter and leave PHP blocks wayyyy too frequently (for my taste). In this case, it was your undoing.
What about using just ONE code block? For example:
<?php
if (mysql_num_rows($result_bookdetail) > 0)
{
while ($row = mysql_fetch_object($result_bookdetail))
{
echo "<table border=0 bgcolor=#ffffff width=80%>"\n;
echo " <tr>\n";
echo ' <td colspan=2><b class="black6">'
. $row->title
. "</b><br><br></td>\n";
etc..
?>
Take care,
Nik
http://www.bigaction.org/
|
|

March 5th, 2004, 04:16 AM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, Nikolai. Thanks for your help. And thanks for mentioning about the repeating php blocks. Well, I am still learning how to do PHP myself.
I do not understand the "\" symbol. Can you explain to me what ie means? and the {} symbol too.
echo "<a href=\"showcart.php?id={$id}\">Add to cart</a>";
regards,
major
|
|

March 5th, 2004, 05:42 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
The "\" indicates an escape:
http://www.php.net/types.string
echo "this " is bad because PHP thinks you ended the string there";
echo "this \" is good because PHP knows that it needs to PRINT the quote instead of using it as the end of the string";
I'm looking into the "{}", perhaps Nik can help you...
I'll let you know if I find out.
----------
---Snib---
----------
|
|

March 5th, 2004, 05:49 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Ah, I found it.
Same page about the strings, a little ways down (use find(on this page) for "braces").
PHP uses the {} to separate variables from text.
Code:
// if the variable is $page
echo "these web $pages are great"; // PHP thinks it is a variable called $pages
echo "these web {$page}s are great"; // PHP knows $page is the variable
echo "these web " . $page . "s are great"; // this also works, although it is a little longer
I hope I have helped you,
----------
---Snib---
----------
|
|
 |