Jump to content

[SOLVED] A Little Help for the Confused


hcdarkmage

Recommended Posts

No, I do not want those values to be random.  Those are the Dealer ID numbers that are assigned through the database.  Those are permanent.

$Top[] = BuyNow(85, 'dealersite.com');
$Top[] = BuyNow(841, 'dealersite.com');
$Top[] = BuyNow(877, 'dealersite.com');
$Top[] = BuyNow(778, 'dealersite.com');
$Top[] = BuyNow(1505, 'dealersite.com');
$Top[] = BuyNow(57, 'dealersite.com');
$Top[] = BuyNow(209, 'dealersite.com');
$Top[] = BuyNow(1260, 'dealersite.com');
$Top[] = BuyNow(1532, 'dealersite.com');
$Bottom[] = BuyNow(189, 'dealersite.com');
$Bottom[] = BuyNow(690, 'dealersite.com');
$Bottom[] = BuyNow(467, 'dealersite.com');
$Bottom[] = BuyNow(1388, 'dealersite.com');
$Bottom[] = BuyNow(1161, 'dealersite.com');

These numbers are referenced here:

function BuyNow($id, $site){
global $db;

if(($Info = $db->get_row('SELECT * FROM dealers WHERE DealerID = '.$id.'')) != '' ){
	$dealer = $Info->DealerName;
	$web = $Info->Website;
	}

 

It tells the file to go into the database, take the dealer ID # and the associated properties that I request from it, add the landing page that I want to go to when clicked and assign those to $Top and $Bottom.

 

As you can see $Top and $Bottom are not given an array value (hence the []), but when the code is activated, they are assigned a value (0 - 9 for $Top and 0 - 5 for $Bottom) automatically and called into the corresponding slot that is elected on the form page ([var.$Top.1.dealer] etc.).

 

Given that as it is, I would like to randomly pick from $Top and $Bottom a different dealer to fit into those corresponding slots in the form (the values $Top[0], $Top[1] etc, be random).  Clear as mud?  The associative arrays that I am pulling need to be assigned a random number for insertion into the form page.

 

At least to the best of my knowledge, that is how this is supposed to work.  Given that I am not entirely sure how to make the request clearer, any questions can be posted here.  ;D

Link to comment
Share on other sites

Definitely clear as mud.

 

Given your syntax of [var.$Top.idx.dealer] in the template, where idx represents one of the possible array indexes (0 through 8 ), are you saying you want the index chosen by the template engine to be random?

 

You do have a clear understanding of the differences between associative and numeric arrays and why the [] operator behaves as it does?

Link to comment
Share on other sites

are you saying you want the index chosen by the template engine to be random?

I think that is what I am saying.  I know it can be done in the PHP script because I have used it before (on single item arrays), but I am not sure how to do it in this case.

 

You do have a clear understanding of the differences between associative and numeric arrays and why the [] operator behaves as it does?

Is this supposed to be a question or statement?

 

Like I have stated before, thank you for trying to help me understand what I am doing.  the best teachers are the ones that don't lose their patience with the beginners.  ;D

Link to comment
Share on other sites

<?php
  // Following is a single item array as it has a single item.
  $singleItemArr = Array( 5 );

  $arr1 = Array();
  $arr1[] = 1;
  $arr1[] = 2;
  $arr1[] = "Three";

  $arr2 = Array();
  $arr2[] = Array("hi", "bye");
  $arr2[] = Array("there", "here");
  $arr2[] = Array("us", "them");

  // $arr1 and $arr2 are both arrays and neither of them is a "single element array"
  // There is NO REASON you can't do the following:
  echo "<pre>" . print_r(shuffle($arr1), true) . "</pre>";
  echo "<pre>" . print_r(shuffle($arr2), true) . "</pre>";
?>

 

I'm making a point out of this because you seem to think there is a difference between an array where each element is a single string, integer, etc. and an array in which each element is another array.  Even though an array can and usually does represent multiple values collected together, an array as a whole represents a single value just like any other string, integer, floating point value, etc.

 

Anyways, I think the problem is narrowed down to you want display the elements of an array of size N in random order but you are getting confused by your template engine.  Like I originally stated, I don't have much experience with templating engines and you are also using one developed in-house, so it's even harder to get support for it.

 

Here is what I do know.  Most templating engines provide a way of performing logical tests and looping.  Most of them do not provide any mechanism to call PHP functions because it is easier to do that from outside the templating engine to begin with.  So trying to put the logic of randomizing your array of arbitrary length into the templating engine is a no-no.

 

Before you go on, you should find out if your template engine supports looping within the template.

Link to comment
Share on other sites

MY HELL! Roopurt, my friend, you are a genius!  I was doing it wrong the whole time (as I slap my forehead and say "DUH").

shuffle($Top);
shuffle($Bottom);

 

This is the code I have been doing wrong for the randomization.  I had it:

shuffle $Top;
shuffle $Bottom;

 

Why didn't I see it before?  Thank you, THANK YOU, THANK YOU!!!!!

 

This post is officially SOLVED!!!!

Link to comment
Share on other sites

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.