Jump to content


Photo

Editing A Function?


  • Please log in to reply
9 replies to this topic

#1 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 29 June 2006 - 02:42 PM

Hi,

I am building a shopping cart, which works great on my localhost. but when i upload it to my server i was getting errors, i have fixed most of them and now just need to get rid of any object code in my scitpt.

Could someone please help?

Heres the error
Fatal error: Call to a member function on a non-object in D:\Domains\spirestest.com\wwwroot\login2\cart\inc\functions.inc.php on line 39

Heres the code

function showCart() {

$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
echo '<form action="cart.php?action=update" method="post" id="cart">';
echo "<center>";
echo "<TABLE width=\"600\" cellpadding=\"0\" cellspacing=\"0\">";
echo
"<TR bgcolor=\"#CCCCCC\">
<TD class=\"Title\">Track</TD>
<TD class=\"Title\">Title</TD>
<TD class=\"Title\">Price</TD>
<TD class=\"Title\">Amount</TD>
<TD class=\"Title\">Total</TD>
<TD class=\"Title\">Remove</TD>
</TR>\n"
;
foreach ($contents as $id=>$qty) {

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);



echo '<tr>
<td><br>'.$title.' </td><td><br> '.$author.'</td>
<td><br>&pound;'.$price.'</td>
<td><br><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>
<td><br>&pound;'.($price * $qty).'</td>

<td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td>

</tr>';
$total += $price * $qty;
}
echo '</table>';
echo "</center>";
echo "<br><hr>";
echo '<table>
<tr>
<td align=\"right\"><span>Grand total: <strong>&pound;'.$total.'</strong></span></td>&nbsp;
</tr>
<tr>
<td align=\"right\"><div><button type="submit">Update cart</button></div></td>
</tr>
</table>';
echo '</form>';
} else {
echo '<p class="loginBox_text">You shopping cart is empty.</p>';
}
echo "</TABLE>\n";
return join('',$output);
}


Thanks for all of your help. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 02:47 PM

It sounds like the error is coming from when you try to query the database. It is saying that $db is not an object. I don't see it being defined anywhere in your script so I think that is the problem. If $db is supposed to be a database abstraction object you need to include the class file and set it with $db = new DB(); or whatever.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 29 June 2006 - 02:50 PM

Hay, I have got abit further,

Is there another way of doing this

$result = $db->query($sql);
$row = $result->fetch();
extract($row);

Not in objects??????


Thanks [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Hi buyocat

I really dont know much about objects.
The script was connecting the new db() bla bla bla.
But i'm trying to convert this so i doesn't need to .


Is this a bad idea?

How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#4 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 02:57 PM

There are a number of ways to skin this cat. I suggest using a PEAR package, such as MDB2, which will handle most everything for you and give you more flexibility. Just in case you are worried about a learning curve, getting it running is as simple as including the file and following the (admittedly sparse) instructions/examples on PEAR. If you don't want to take the plunge however you can accomplish it with something along these lines...

$dbc = mysql_connect(various information here);
// check that the connection was established
$result = @mysql_query(a query here);
// check that the result is what you wanted
// do stuff

I'll let you look up those functions on php.net. You can probably just enter in...
php.net/mysql_connect
php.net/mysql_query
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#5 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 29 June 2006 - 03:11 PM


HI thanks for your help.

I have already converted the inc.db page and that connects fine.


<?php
$host = 'europa';
$user = '*******';
$pass = '*******;
$db = 'jeff_cart';

$connect = mysql_connect($host, $user, $pass) or die ("could not connect");
mysql_select_db($db) or die ("could not select db");

?>

However, i just need to now convert this simple bit of code, so it none object related.

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);


I been playing around with a few things here. Which removes the error, but no resutls are displaying?

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
extract($row);

goto [a href=\"http://www.spirestest.com/login2/cart/cart.php\" target=\"_blank\"]My Webpage[/a]

Thanks once again for all your help.
Its nice to be in the hands of masters.


How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#6 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 03:15 PM

For the result try this:
while ($data = mysql_fetch_array($result, MYSQL_NUM)) {
echo $data[0];
}

that will echo out the first column of the first row fetched.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#7 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 29 June 2006 - 03:20 PM

No Luck,

However, i'm not to sure if i put it in the right place.

Could you please take a look?

function showCart() {

$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
echo '<form action="cart.php?action=update" method="post" id="cart">';
echo "<center>";
echo "<TABLE width=\"600\" cellpadding=\"0\" cellspacing=\"0\">";
echo
"<TR bgcolor=\"#CCCCCC\">
<TD class=\"Title\">Track</TD>
<TD class=\"Title\">Title</TD>
<TD class=\"Title\">Price</TD>
<TD class=\"Title\">Amount</TD>
<TD class=\"Title\">Total</TD>
<TD class=\"Title\">Remove</TD>
</TR>\n"
;
foreach ($contents as $id=>$qty) {

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = mysql_query($sql);
$row = mysql_fetch_row($result);

while ($data = mysql_fetch_array($result, mysql_num)) {
echo $data[0];



echo '<tr>
<td><br>'.$row['title'].' </td><td><br> '.$row['author'].'</td>
<td><br>&pound;'.$row['price'].'</td>
<td><br><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>
<td><br>&pound;'.($price * $qty).'</td>

<td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td>

</tr>';
$total += $price * $qty;
}
}
echo '</table>';
echo "</center>";
echo "<br><hr>";
echo '<table>
<tr>
<td align=\"right\"><span>Grand total: <strong>&pound;'.$total.'</strong></span></td>&nbsp;
</tr>
<tr>
<td align=\"right\"><div><button type="submit">Update cart</button></div></td>
</tr>
</table>';
echo '</form>';
} else {
echo '<p class="loginBox_text">You shopping cart is empty.</p>';
}
echo "</TABLE>\n";
return join('',$output);
}


Cheers Buyocat
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#8 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 03:25 PM

What error are you getting when you run this? If it isn't printing an error try echoing out something from the top of your script working down until you hit where the error is. Finally, if the error is with the mysql try this:

$result = mysql($query) or die (mysql_error()); Or something like that

Looking at your code, I'm a little confused about what you're doing after the query. It looks like it would try to echo out a result column but then move past that and use mysql_fetch_row. I don't know how mysql_fetch_row works, but it would be worth print_r - ing the $row variable to see what it is being set to.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#9 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 29 June 2006 - 03:38 PM

Sorry to be such a pain.

I'm not getting an error now.
Its just not displaying any $*****
Everything else in the function is being displated

The page before is working, and the edit page is working,
Its just this function.

Goto and you will see,
[a href=\"http://www.spirestest.com/login2\" target=\"_blank\"]My Webpage[/a]

Thanks
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#10 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 29 June 2006 - 03:53 PM

Hay, Thanks for your help.

I HAVE DONE IT! WWOOOO


He is the solution,


foreach ($contents as $id=>$qty) {

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

echo '<tr>
<td><br>'.$row['title'].' </td><td><br> '.$row['author'].'</td>
<td><br>&pound;'.$row['price'].'</td>
<td><br><input type="text" name="qty'.$row['id'].'" value="'.$qty.'" size="3" maxlength="3" /></td>
<td><br>&pound;'.($row['price'] * $qty).'</td>

<td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td>

</tr>';
$total += $row['price'] * $qty;

}


Thanks once again. [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users