|
-
Aug 19th, 2005, 03:16 PM
#1
Thread Starter
Addicted Member
[RESOLVED] PHP newbie help
Hi everyone,
I am sort of new to PHP and i have a few questions...
1. $_SESSION["cart"] is supposed to be an array which holds an array variable which in the code its supposed to be $arow...
is there anything wrong with this code?
Code:
$arow = array($id, $qty, $prc);
if(isset($_SESSION["cart"]))
{
$found = 0;
foreach($_SESSION["cart"] as $value)
{
if($id == $value[0])
{
$found = 1;
}
}
if($found == 0)
{
count++;
$_SESSION["cart"][] = $arow;
}
}
else
{
$count = 1;
$_SESSION["cart"][] = $arow;
}
2. When having a link, or a button, how does one go about so that a php function is called when that button is pressed or link is clicked ?
Thanks in advanced
Last edited by Osiris; Aug 19th, 2005 at 09:06 PM.
؊Ϯϊ
-
Aug 19th, 2005, 03:22 PM
#2
Re: PHP newbie help
In the href attribute of you link, you can append a query string with variables. These variables can then be used in your PHP script:
HTML Code:
<a href="my_page.php?action=deleteitem&item=2">Delete Item</a>
You PHP codewould look somnething like this:
PHP Code:
if (isset($_GET['action'])) {
switch($_GET['action']) {
case 'deleteitem':
unset($_SESSION['cart'][(int) $_GET['item']]);
}
}
-
Aug 19th, 2005, 03:36 PM
#3
Thread Starter
Addicted Member
Re: PHP newbie help
well, I wanted to call a function in that link click rather than to refer to the php webpage again...
is there a way to call php functions within a button or link?
-
Aug 19th, 2005, 03:53 PM
#4
Re: PHP newbie help
Not in PHP - PHP is a server side scripting language and as such there needs to be communication between the client and the server when a request is made. However, there is nothing stoping you from invoking a PHP function by using a link which points to the same page.
PHP Code:
?>
<a href="<?php echo($_SERVER['PHP_SELF']) ?>?action=deleteitem&item=2">Delete Item</a>
It is possible through use of a technology called AJAX], to use Javascript in conjunction with server side scripting lanuages such as PHP, to have it seem as if the user is interacting with the script. Google Suggest uses this technology to display as you type search results.
In reality, each keypress in the search field triggers a Javascript event, which in turn fetches some data from the server which dynamically updates a portion of the HTML page.
-
Aug 19th, 2005, 03:56 PM
#5
Thread Starter
Addicted Member
Re: PHP newbie help
what i am trying to do is call this function via link or button click...
Code:
function addItem($id, $qty, $prc)
{
$arow = array($id, $qty, $prc);
if(isset($_SESSION["cart"]))
{
$found = 0;
foreach($_SESSION["cart"] as $value[])
{
if($id == $value[0])
{
$found = 1;
}
}
if($found == 0)
{
count++;
$_SESSION["cart"][] = $arow;
}
}
else
{
$count = 1;
$_SESSION["cart"][] = $arow;
}
}
but for some reason I am getting errors in this function...
Is there supposed to be brackets were the red brackets are? Is the syntax in that function correct? because there seems to be an error around the foreach loop and I don't know what it is...
-
Aug 19th, 2005, 04:04 PM
#6
Re: PHP newbie help
The code above would generate an error. You don't need the brackets after $value. What error are you getting?
-
Aug 19th, 2005, 04:13 PM
#7
Thread Starter
Addicted Member
Re: PHP newbie help
 Originally Posted by visualAd
The code above would generate an error. You don't need the brackets after $value. What error are you getting?
I don't know, is just that the page appears blank, and when I remove that code the page is viewable.
-
Aug 19th, 2005, 05:09 PM
#8
Re: PHP newbie help
Can you post the whole script? The function itself doesn't produce any output, this suggests to me that the error is somewhere else in your code.
Also, to see the errors turn on error reporting by putting this line at the top of your script:
PHP Code:
error_reporting(E_ALL);
-
Aug 19th, 2005, 06:29 PM
#9
Thread Starter
Addicted Member
Re: PHP newbie help
I know the script doesn't produce any output, but if you were to put it within HTML code the HTML code would be ignored...
So, I think the problem is near the foreach loop because whenever I remove the foreach loop the webpage can be seen...
Code:
<html>
<body>
Hello World
<?php
session_start();
function addItem($id, $qty, $prc)
{
$arow = array($id, $qty, $prc);
if(isset($_SESSION["cart"]))
{
$found = 0;
foreach($_SESSION["cart"] as $value[])
{
if($id == $value[0])
{
$found = 1;
}
}
if($found == 0)
{
count++;
$_SESSION["cart"][] = $arow;
}
}
else
{
$count = 1;
$_SESSION["cart"][] = $arow;
}
}
?>
</body>
</html>
-
Aug 19th, 2005, 06:36 PM
#10
Thread Starter
Addicted Member
Re: PHP newbie help
Ok, I found the reason that was causing that error...
Code:
if($found == 0)
{
count++; <--$count++;
$_SESSION["cart"][] = $arow;
}
But I still figured out how to call this function from a link or button...
-
Aug 19th, 2005, 09:09 PM
#11
Thread Starter
Addicted Member
Re: [RESOLVED] PHP newbie help
visualAd, thanks...
I finally got it to work, I did have to use what you told me to use... <a href="page.php?id=1&var2=2"> and from then I redirect it back to the page I was using $HTTP_REFERER
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|