Manipulating Multiple Values from a Link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zoonix
Forum Newbie
Posts: 2
Joined: Sun Nov 15, 2009 5:35 pm

Manipulating Multiple Values from a Link

Post by zoonix »

I've been trying to work out an issue and I suspect that I'm making it much more difficult than it need be.

What I'm doing is pulling from an xml feed a list of video titles and ID's and displaying them on a page. Each video has a link to "Add to Cart" link that adds it's ID to a cart array. The ID is then displayed in a checkout box with a "Remove" link next to it.

So the problem is that I want to display the Title next to the "Remove" button, but I can't for the life of me figure out how to pass the ID & Title through the link in a way that doesn't screw everything up.

I want to end up with a set of Title values to display and a separate set of ID values to send back when they hit "Check Out". It doesn't seem like it should be all that complicated, but after a couple days searching and failed experiments, I'm stumped!

I'm sure I'm not trying to reinvent the wheel here, what am I missing?

Thanks!

I've worked on it some more and this is the best I've come up with, but it seems rather ham-fisted. Any better suggestions on how to go about this???

Code: Select all

 
<?php
    session_start();
    $cart = $_SESSION['cart'];
    $action = $_GET['action'];
    switch ($action) {
        case 'add':
            if ($cart) {
                $cart .= ','.$_GET['id'];
            }
            else {
                $cart = $_GET['id'];
            }
            
        break;
        case 'delete':
            if ($cart) {
                $items = explode(',',$cart);
                $newcart = '';
                foreach ($items as $item) {
                    if ($_GET['id'] != $item) {
                        if ($newcart != '') {
                            $newcart .= ','.$item;
                        }
                    else {
                        $newcart = $item;
                    }
                }
            }
            $cart = $newcart;
        }
        break;
        case 'clear':
            global $cart;
            unset($cart);
        break;
    }
    if ($cart) {
        $items1 = explode(',',$cart);
        $items2 = array_unique($items1);
        $cart = implode(",",$items2);
    }
    $_SESSION['cart'] = $cart;
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
 
<?php
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);        
        $commacart = str_replace(":",",",$cart);
        $itemsplit = explode(',',$commacart);
        $count = round(count($itemsplit)/2-1);
        
        $a = 0;
        for ($i = 0; $i <= $count; $i++) {
            $cartid[] = $itemsplit[$a];
            $a = $a+2;
        }
        
        $a = 1;
        for ($i = 0; $i <= $count; $i++) {
            $carttitle[] = $itemsplit[$a];
            $a=$a+2;
        }
        
        function createlink($b) {
            return('id='.$b);
        }
        $chkoutarray = array_map("createlink", $cartid);
        $chkoutlink = implode(";",$chkoutarray);
        
        echo "
            <table width=\"190px\">";
                for ($i = 0; $i <= $count; $i++) {
                    echo "
                        <tr>
                            <td>$carttitle[$i]</td>
                            <td width=\"50px\"><a href=\"/pages/test.php?action=delete&id=$items[$i]\" class=\"r\">Remove</a></td>
                        </tr>";
                }
            echo "</table><br />";
            
            echo "
                <table width=\"190px\">
                    <tr>
                        <td><a href=\"/pages/test.php?action=clear\">Clear Cart</a></td>                                    
                        <td><a href=\"href='https://secure.foo.com/bill.cgi?storeid=12345;$chkoutlink;CheckOut=1'\">Check Out</a></td>
                    </tr>
                </table>";
        }
    else { echo "<p>No Cart</p>"; }
?>
 
<?php
    $objDOM = new DOMDocument();
    $objDOM->load("https://foo.com/bar.php?studio_id=12345");
                
    $clip = $objDOM->getElementsByTagName("clip");
                
    foreach( $clip as $value ) {
        $vid = $value->getElementsByTagName("id");
        $vid  = $vid->item(0)->nodeValue;
        $title = $value->getElementsByTagName("title");
        $title  = $title->item(0)->nodeValue;
        
        echo 'ID = '.$vid.'<br />';
        echo 'Title = '.$title.'<br />';
        echo '<br />';
        
        $cartlink = "<a href=\"/pages/test.php?action=add&id=$vid:$title\" class=\"r\">Add</a><br />";  
        echo $cartlink;
    }                   
?>
 
zoonix
Forum Newbie
Posts: 2
Joined: Sun Nov 15, 2009 5:35 pm

Re: Manipulating Multiple Values from a Link

Post by zoonix »

I seem to have everything working the way I want, but it still seems more complicated than need be. Any suggestions for improvement are welcome!
Post Reply