Jump to content

Recommended Posts

I have a code that changes my headers every time someone refreshes or changes pages in the website.  It was easy because I have 12 headers with 3 web pages they point to.

$Num = rand(1,12);
$Img = ''.$Num.'';
if ($Img<=4){
$Sboost = 'ViewProduct.php?ID=123';
}elseif ($Img>=9){
$Sboost = 'Misc.php?Page=AboutUs';
}else{
$Sboost = 'ViewProduct.php?ID=126';
}

Yes the header image titles are numbers, not names.

 

Now my question is can you make the numbers in question ranges?  I ask because I am adding 4 more headers and a new landing page but I am not sure how to code the 4 other headers to the 4th landing page.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/63076-solved-help-using-number-ranges/
Share on other sites

What do you mean by headers? And why are you using such a large range for random when you're using about the same amount of weight per url? You can simplify it some:

$list=array("ViewProduct.php?ID=123","Misc.php?Page=AboutUs","ViewProduct.php?ID=126");
$index=(int)rand(0,count($list)-1); //Get a random index inside there
$Sboost=$list[$index];

What are the numbers in question?

What I meant are the banners that have the logo and all that to announce the site:http://wilsonelectronics.com/.  I'm sorry if I confused you by saying headers.  I meant banners.

 

There are 12 different banners that change when people go to a new page in the site, refresh or revisit.  There are 3 different pages that are split between the 12 banners (4 banners lead to each page).  If you click on the link and refresh every now and then, you will see what I mean.

 

I wrote the code to make it so if a certain banner shows up (1-4, 5-8, 9-12) then when a user clicks on the right side of the banner, it goes to the specified page.  Pretty soon I will be adding 4 more banners (13-16) that will go to a separate landing page (new product being added).

 

In the code I used the if...elseif statement to make sure that the advertisement on the banner goes to the right page when clicked.  The banners are named 1-12 and each set of 4 have a different product advertised on them.  I am asking if there is a way to set ranges in the if..else statement to add the fourth set of 4 banners.

 

Clear as mud?

 

If you have any other remarks or suggestions, I am open!

Now I'm just confused.

 

The banners use numbers (1.jpg, 2.jpg, 3.jpg, etc.).  Each set of 4 images have an ad that points to a specific product when clicked.

 

1-4.jpg -> reference 1

5-8.jpg -> reference 2

9-12.jpg -> reference 3

soon to be 13-16.jpg -> reference 4

 

How can I implement the code you provided to reflect this?

 

I know that in the code I wrote, I specifically told it if the numbers are below or equal to 4 then go to reference 1, if the numbers are above or equal to 9 then go to reference 3 and all others go to reference 2.

 

The thing that confuses me is that I don't see the same referencing in your code.  Could you clarify it a little for me?

 

If that doesn't work, how can I change my code to do the same (i.e. number ranges). Can you do number ranges?

you can specify ranges by specifying a lower and an upper bound in each conditional, like so:

 

<?php
if (1 <= $x && $x <= 4)
{
  // ref 1
}
else if (5 <= $x && $x <= 
{
  // ref 2
}
else if (9 <= $x && $x <= 12)
{
  // ref 4
}
?>

 

.. and so on.  i'm going to do a bit of investigation into switch(), because that would really make things easier for you; i just can't remember the degree to which cases can be complex.  it would be nice if we had MySQL syntax where you could just say CASE x BETWEEN 1 AND 4.

I see how that would work now.

 

$Num = rand(1,16);
$Img = ''.$Num.'';
if (1 <= $Img && $Img <= 4){
$Sboost = 'reference1';
}elseif (5 <= $Img && $Img <= {
$Sboost = 'reference2';
}elseif (9 <= $Img && $Img <= 12){
$Sboost = 'reference3';
}elseif (13 <= $Img && $Img <= 16){
$Sboost = 'reference4';
}

 

Thank you Akitchin!  I knew you could do number ranges, but I couldn't remember how.  ;D

it would be handy if one could specify them much the way you would in math, without the && operator, but them's the breaks.  if you want a more dynamic method, you could use a function like the following:

 

<?php
function get_from_ranges($search, $array_ranges)
{
  // go through each range, exploding for the left and right limits
  foreach ($array_ranges AS $range => $ref)
  {
    list($lb, $rb) = explode('-', $range)
    if ($lb <= $search && $search <= $rb)
    {
      return $ref;
    }
  }

  // return a problem if it wasn't found
  return 'Not in ranges specified.';
}

$ranges = array('1-4' => 'reference1', '5-8' => 'reference1', '9-12' => 'reference1');
$search = rand(0,16);
echo 'number is: '.$search.', its reference becomes: '.get_from_ranges($search, $ranges);
?>

 

saves you from adding all those extra if()s.  keep in mind you may want some typecasting to make sure they're numbers - you could probably even use this for letters, if you want to cast them into their character # (although I think it does this automatically).

I think for the application it is being used for, the first method you suggested should work.  It only changes the top banner when people go through the site or refresh the page.  One of these days I'll learn to do it in Flash and solve some of my programming issues!  ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.