Home > General > Affiliate Marketing Forum

Dynamic Product Placement PHP - Need Help (5)


10-12-2011 01:59 PM #1 atlepe (Member)
Dynamic Product Placement PHP - Need Help [SOLVED]

I want to display an image based on what is in the keyword string, so far I'm able to do just that, but how do I make a condition to print something else if one of those particular words don't exist in the string?

Here's the code I have so far:

[PHP]<?php
// Get the keyword.
$keyword = $_GET['t202kw'];

// Display the image, the following code goes inside the html.
// Example: <img src="path/<?php the code ?>" />
<?php
$product = strripos($keyword, "kw1"); // Find the product.
if ($product > 1 ) {
echo "image1.gif"; // Print the product.
}
$product = strripos($keyword, "kw2"); // Find another product.
if ($product > 1 ) {
echo "image2.gif"; // Print another prooduct.
}
?>[/PHP]

Also, is there any way to turn this into a function or something so I don't have to paste the whole code inside the html everytime I want to display a dynamic image?


10-12-2011 02:38 PM #2 bbrock32 (Administrator)

For your first question , add this at the end :

[PHP]
else{
echo "no img found";
}
[/PHP]

As for your second question , you need to declare a function like this :

[PHP]
function ShowImg($keyword){
$ret="0";
$product = strripos($keyword, "kw1"); // Find the product.

if ($product > 1 ) {
$ret= "image1.gif"; // Print the product.
}
$product = strripos($keyword, "kw2"); // Find another product.
else if ($product > 1 ) {
$ret= "image2.gif"; // Print another prooduct.
}
else{
//When no kw if found
$ret= "default.gif";
}
return $ret;
}
[/PHP]

And you will call it like this :

[PHP]
$img=ShowImg($keyword);
echo $img;
[/PHP]

Hope this helps


10-12-2011 06:38 PM #3 atlepe (Member)

Thanks for helping out.

The first code is working fine on kw2, but if kw1 is in the string it returns both image1 and image not found, also, do you know how I can add a third product to this?

When I do add a third product it works fine when the keyword for it is in the string, but the other two returns both the keyword and the default.

When the last 'else' is left out all the keywords are working fine.

I'm using this:

[PHP]<?php
$product = strripos($keyword, "kw1"); // Find the product.
if ($product > 1 ) {
echo "image1.gif"; // Print the product.
}
$product = strripos($keyword, "kw2"); // Find another product.
if ($product > 1 ) {
echo "image2.gif";
}
$product = strripos($keyword, "kw3"); // Find another product.
if ($product > 1 ) {
echo "image3.gif";
}
else{
echo "no img found";
}
?>[/PHP]

Returns "no img foundimage1.gif" if kw1 is in the string.

The second code:

Parse error: syntax error, unexpected T_ELSE on line 11
which is line 8 in the code you provided.

So I turned the 'else if' on that line into 'if' and the page loads fine, but it always prints default.gif even if the keyword is in the string, no idea what's going on.

I've been busting my balls for 2 days trying to make this work, so I appreciate any help you can get me on this.


10-12-2011 11:32 PM #4 atlepe (Member)

I think there's a problem with the way they are stacked because the last keyword always works, but if any of the other keywords are present it triggers both default and the keyword.

With the function it's kinda opposite: the last keyword works, but it skips the two first.

So how do I make the code only execute if the other conditions aren't met?


10-13-2011 01:04 AM #5 atlepe (Member)

Got it, no idea what was going on in that other one but here's a working code if anyone else needs it for something.

[PHP]if ($product = strpos($keyword, "kw1")) { // Get the product.
echo "image1.gif"; // Print the product.
} elseif ($product = strpos($keyword, "kw2")) { // Get another product.
echo "image2.gif"; // Print another product etc...
} elseif($product = strpos($keyword, "kw3")) {
echo "image3.gif";
} else {
echo "empty"; // No product.
}[/PHP]

And here it is as a function:

[PHP]function ShowImg($keyword){ // Declare the function.
$ret="0"; // ???
if ($product = strpos($keyword, "kw1")) { // Get the product.
$ret = "image1.gif"; // Print the product.
} elseif ($product = strpos($keyword, "kw2")) { // Get another product.
$ret = "image2.gif"; // Print another product etc...
} elseif($product = strpos($keyword, "kw3")) {
$ret = "image3.gif";
} else {
$ret = "empty"; // No product.
}
return $ret;
}
// Call the function.
$img=ShowImg($keyword);
echo $img;[/PHP]

If your code looks a little messy with all that stuff in it you can save it as a separate php file and include it:

[PHP]include ("dynamics.php");[/PHP]

Save it in the same folder as your index file.

Don't forget the keyword:

[PHP]$keyword = $_GET['t202kw'];[/PHP]

The solution:

http://php.net/manual/en/control-structures.switch.php

After that it was just a matter of extracting the keyword from the string and giving it a condition if it exists or something. Not bad for my first attempt at php

*unloads shotgun


Home > General > Affiliate Marketing Forum