Jump to content

Setting a php link as variable...???


inquisitive

Recommended Posts

Ok here is my code:

 

<a href="cart.php?action=add&id='.$row['id'].'">

 

ok now i am prolly really just making this more complicated than it is...but how do i make this a variable...

 

What I am trying to accomplish is that my shopping cart page calls this using $_GET['action']....but I need to make it so that it only executes my function if the link is pushed.  In order to do that i believe i need to setup an (isset['$action']) or something similar....some help please???

Link to comment
https://forums.phpfreaks.com/topic/125939-setting-a-php-link-as-variable/
Share on other sites

If I understand what you mean, then you could probably do this using sessions and substr_count()

 

<?php

if(substr_count($_SESSION['shopping_basket'],$_GET['id']) == 0)) {
  $_SESSION['shopping_basket'] = $_SESSION['shopping_basket'].";".$_GET['id'];
}

?>

 

That'll only add items to the shopping basket, based on the value in the url, if value isn't already in the session.

 

Also stores multiple items in the shopping basket, which you can separate using explode().

 

Is that what you were after?

well actually sort of...i literally want to turn that link i posted into a variable then way within my functions page I can write a script to execute based on that being clicked...right now it just calls a function that automatically runs through the cart...if you need to see some code I can put it up on phpfi.com ...

 

So when you click the link at the moment, a function is called that looks at everything in the cart, but you just want it to look at one thing in particular?

 

If you want the link in a variable, all you need to do is

<?php $link = "<a href='cart.php?action=add&id=".$row['id']."'>Link</a>"; ?>

 

But I'm pretty sure that isn't what you mean.

 

May need to look at some of your code!

Ok, Still trying to get my head round your code, but 1st of all, there is an error on line 28. (Looks like you just copied the code that I last posted in, so partially my fault I suppose).

 

<?php

$output[] = '<li>"'.$row['product_name'].'" by '.$row['manufacturer'].': $'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to Cart</a></li>';

?>

 

Which function is it that you are having a problem with it, or what is it that you need the function to do?

 

You know you can pass values to functions?

 

<?php

function yourFunction($value,$vaule2) {
// Your function code.
}

// And to call the function
yourFunction("The 1st thing you want to pass to it","The 2nd thing");

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.