Results 1 to 11 of 11

Thread: [RESOLVED] PHP newbie help

  1. #1

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    [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.
    ؊Ϯϊ

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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']]);
        }

    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    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?
    ؊Ϯϊ

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    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...
    ؊Ϯϊ

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP newbie help

    The code above would generate an error. You don't need the brackets after $value. What error are you getting?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    Re: PHP newbie help

    Quote 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.
    ؊Ϯϊ

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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); 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    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>
    ؊Ϯϊ

  10. #10

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    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...
    ؊Ϯϊ

  11. #11

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    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
  •  



Click Here to Expand Forum to Full Width