Jump to content

[SOLVED] A Little Help for the Confused


hcdarkmage

Recommended Posts

I have a problem with my input variables.  I am not entirely sure why they don't input the way I ask them to.  In my HTML file I set up the call variables [something.info], but my code is not placing them.  I have gone through the documentation that I have found (not understanding all of it), and still can't quite put my finger on the problem.

 

I created this PHP file ('Funct/buynow.php):

<?php

define('RelativePath', $_SERVER['DOCUMENT_ROOT']);
include_once('Classes/Template.php');

// Create a class
class BuyNow{

// Create the Function
function BuyNow($dealer, $web, $site, $rank){

// Define the variables $dealer, $web, $site and $rank	
	$New[] = BuyNow('dealer1', 'web1', 'http://www.site1.com', '1');
	$New[] = BuyNow('dealer2', 'web2', 'http://www.site2.com', '1');
	$New[] = BuyNow('dealer3', 'web3', 'http://www.site3.com', '1');
	$New[] = BuyNow('dealer4', 'web4', 'http://www.site4.com', '1');
	$New[] = BuyNow('dealer5', 'web5', 'http://www.site5.com', '1');
	$New[] = BuyNow('dealer6', 'web6', 'http://www.site6.com', '1');
	$New[] = BuyNow('dealer7', 'web7', 'http://www.site7.com', '1');
	$New[] = BuyNow('dealer8', 'web8', 'http://www.site8.com', '1');
	$New[] = BuyNow('dealer9', 'web9', 'http://www.site9.com', '1');
	$New[] = BuyNow('dealer10', 'web10', 'http://www.site10.com', '0');
	$New[] = BuyNow('dealer11', 'web11', 'http://www.site11.com', '0');
	$New[] = BuyNow('dealer12', 'web12', 'http://www.site12.com', '0');
	$New[] = BuyNow('dealer13', 'web13', 'http://www.site13.com', '0');
	$New[] = BuyNow('dealer14', 'web14', 'http://www.site14.com', '0');
}
}

// Define Plug in Variables
$New['dealer'] = $dealer;
$New['web'] = $web;
$New['site'] = $site;
return $New;

// Randomize the array	
shuffle($New);
return $New;

// Separation of dealers
if($rank = 1){
	$Top[] = $New;
}else{
	$Bottom[] = $New;
}

// Define $Top nad $Bottom
$Top = new BuyNow;
$Bottom = new BuyNow;

$Tpl = new clsTinyButStrong ;

// Make Blocks 'Top' and 'Bottom'
$Tpl->MergeBlock('Top', $Top);
$Tpl->MergeBlock('Bottom', $Bottom);

$Tpl->Show();

?>

 

I showed that it needs to be included in this file (Dealer3.php):

<?php

define('RelativePath', $_SERVER['DOCUMENT_ROOT']);
include_once('Classes/Template.php');
	include_once('Classes/DBHandle.php');
	include_once('Funct/Dealer3.php');
	include_once('Funct/buynow.php');

$RootPath = './';
$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';
	}

$SubTemp = 'Templates/Body/Dealer/Form3.htm';

$Locator = new DealerLocator;

$ReqArray['FirstName'] = '';
$ReqArray['LastName'] = '';
$ReqArray['Phone'] = '';
$ReqArray['Zip'] = '';
$ReqArray['Email'] = '';

if(sizeof($_POST) > 0)
	foreach($_POST as $Key => $Value)
		$$Key = $Value;
else
	foreach($ReqArray as $Key => $Value)
		$$Key = $Value;

if(isset($Locator->Failed))
$Error = $Locator->Failed;
else
$Error = '';

if(isset($Locator->Go)){
	$SubTemp = 'Templates/Body/Dealer/Disp3.htm';
	$RemReq = $Locator->RemReq;	
}

$Tpl = new clsTinyButStrong ;

$Tpl->LoadTemplate('Templates/Main/index.htm') ;

if(isset($Locator->Go))
	$Tpl->MergeBlock('Dealers', $Locator->Dealers);

$Tpl->Show();

?>

 

But it doesn't plug the variables into this file (Templates/Body/Dealer/Form3.htm):

-snip-

<td width="90%">[Top;block=td]
              <table width="100%" border="0" cellpadding="2" cellspacing="2">
                <tr>
                  <td><span class="style13">[Top_1.dealer]</span></a></td>
                </tr>
                <tr>
                  <td><a href="[Top_1.site]" onclick="window.location = '[Top_1.site]';" style="display: block; cursor: pointer; text-decoration: none;">[Top_1.web]</a></td>
                </tr>
              </table>
              <hr />
                <table width="100%" border="0" cellpadding="2" cellspacing="2">
                  <tr>
                    <td><span class="style13">[Top_2.dealer]</span></a></td>
                  </tr>
                  <tr>
                    <td><a href="[Top_2.site]" onclick="window.location = '[Top_2.site]';" style="display: block; cursor: pointer; text-decoration: none;">[Top_2.web]</a></td>
                  </tr>
                </table>
                <hr />

-snip-

 

Any suggestions into what it is exactly I am doing wrong?  If you need more information, I can and will give it to you.  As for what this is supposed to do,  It is supposed to take x amount of dealers and place them in y amount of slots acording to their rank.  Here is a reference to what it should look like: http://wilsonelectronics.com/Dealer.php (the BUY NOW section).

 

Thanks for any and all help.

Link to comment
Share on other sites

I didn't read your entire code mainly because I'm lazy....

 

Do you have a BuyNow function defined out side of the class?  If you call a function inside of a class it looks for it in the global scope unless you do $this->Function().

 

 

Link to comment
Share on other sites

Actually . . . I do not.  Where would I define it?  I usually do small fixings in the code and this is my first time actually trying to get it to do something.  The 3 main files I use are displayed.  Would I define the BuyNow function in the 'Funct/buynow.php' file or the 'Dealer3.php' file?  I don't think I would define it in the HTML file ('Templates/Body/Dealer/Form3.htm').  Then again I have been wrong before. ;)

Link to comment
Share on other sites

All righty.  This is starting to haunt me even more.  I have been trying to figure this out for quite a while, and I don't seem to be making any headway.  Every time I try to input the argument into the 'Dealer.php' file I get error messages:

 . . .
$SubTemp = 'Templates/Body/Dealer/Form3.htm';
      
        $Top = new BuyNow;
        $Bottom = new BuyNow;

$Locator = new DealerLocator;

$ReqArray['FirstName'] = '';
$ReqArray['LastName'] = '';
$ReqArray['Phone'] = '';
$ReqArray['Zip'] = '';
$ReqArray['Email'] = '';

if(sizeof($_POST) > 0) . . .

The error messages are:

Warning: Missing argument 1 for buynow() in /home/content/w/i/l/wilsonelectron/html/Funct/buynow.php on line 10

Warning: Missing argument 2 for buynow() in /home/content/w/i/l/wilsonelectron/html/Funct/buynow.php on line 10

Warning: Missing argument 3 for buynow() in /home/content/w/i/l/wilsonelectron/html/Funct/buynow.php on line 10

Warning: Missing argument 4 for buynow() in /home/content/w/i/l/wilsonelectron/html/Funct/buynow.php on line 10

Fatal error: Call to undefined function: buynow() in /home/content/w/i/l/wilsonelectron/html/Funct/buynow.php on line 13

 

All the code is posted in my first post and I can't see what I am doing wrong.  Please help.

 

If more information is needed, just ask I will offer what I can.

Link to comment
Share on other sites

Here we go.  I made the errors disappear, but I am still getting no where with the plug in portion of the code.  My new 'Funct/buynow.php' file looks like:

<?php

// Create a class
class BuyNow{
	var $dealer;
	var $web;
	var $site;
	var $rank;
}
// Create the Function
function BuyNow($dealer, $web, $site, $rank){

// Define the variables $dealer, $web, $site and $rank	
	$New[] = BuyNow('dealer1', 'web1', 'http://www.site1.com', '1');
	$New[] = BuyNow('dealer2', 'web2', 'http://www.site2.com', '1');
	$New[] = BuyNow('dealer3', 'web3', 'http://www.site3.com', '1');
	$New[] = BuyNow('dealer4', 'web4', 'http://www.site4.com', '1');
	$New[] = BuyNow('dealer5', 'web5', 'http://www.site5.com', '1');
	$New[] = BuyNow('dealer6', 'web6', 'http://www.site6.com', '1');
	$New[] = BuyNow('dealer7', 'web7', 'http://www.site7.com', '1');
	$New[] = BuyNow('dealer8', 'web8', 'http://www.site8.com', '1');
	$New[] = BuyNow('dealer9', 'web9', 'http://www.site9.com', '1');
	$New[] = BuyNow('dealer10', 'web10', 'http://www.site10.com', '0');
	$New[] = BuyNow('dealer11', 'web11', 'http://www.site11.com', '0');
	$New[] = BuyNow('dealer12', 'web12', 'http://www.site12.com', '0');
	$New[] = BuyNow('dealer13', 'web13', 'http://www.site13.com', '0');
	$New[] = BuyNow('dealer14', 'web14', 'http://www.site14.com', '0');
}

// Separation of dealers
if($rank = 1){
	$Top[] = $New;
}else{
	$Bottom[] = $New;
}

// Define Plug in Variables
$Top['dealer'] = $dealer;
$Top['web'] = $web;
$Top['site'] = $site;
return $Top;

$Bottom['dealer'] = $dealer;
$Bottom['web'] = $web;
$Bottom['site'] = $site;
return $Bottom;

$Tpl = new clsTinyButStrong ;

// Make Blocks 'Top' and 'Bottom'
$Tpl->MergeBlock('Top', $Top);
$Tpl->MergeBlock('Bottom', $Bottom);

$Tpl->Show();

?>

 

My 'Dealer3.php' file is:

<?php

define('RelativePath', $_SERVER['DOCUMENT_ROOT']);
include_once('Classes/Template.php');
	include_once('Classes/DBHandle.php');
	include_once('Funct/Dealer3.php');
	include_once('Funct/buynow.php');

$RootPath = './';
$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';
	}

$SubTemp = 'Templates/Body/Dealer/Form3.htm';

$Top = new BuyNow;
$Bottom = new BuyNow;
$Locator = new DealerLocator;

$ReqArray['FirstName'] = '';
$ReqArray['LastName'] = '';
$ReqArray['Phone'] = '';
$ReqArray['Zip'] = '';
$ReqArray['Email'] = '';

if(sizeof($_POST) > 0)
	foreach($_POST as $Key => $Value)
		$$Key = $Value;
else
	foreach($ReqArray as $Key => $Value)
		$$Key = $Value;

if(isset($Locator->Failed))
$Error = $Locator->Failed;
else
$Error = '';

if(isset($Locator->Go)){
	$SubTemp = 'Templates/Body/Dealer/Disp3.htm';
	$RemReq = $Locator->RemReq;	
}

$Tpl = new clsTinyButStrong ;

$Tpl->LoadTemplate('Templates/Main/index.htm') ;

if(isset($Locator->Go))
	$Tpl->MergeBlock('Dealers', $Locator->Dealers);

$Tpl->Show();

?>

 

And of course the HTML page that it should plug into:

<td width="90%">[Top;block=td]
              <table width="100%" border="0" cellpadding="2" cellspacing="2">
                <tr>
                  <td><span class="style13">[Top_1.dealer]</span></a></td>
                </tr>
                <tr>
                  <td><a href="[Top_1.site]" onclick="window.location = '[Top_1.site]';" style="display: block; cursor: pointer; text-decoration: none;">[Top_1.web]</a></td>
                </tr>
              </table>
              <hr />
                <table width="100%" border="0" cellpadding="2" cellspacing="2">
                  <tr>
                    <td><span class="style13">[Top_2.dealer]</span></a></td>
                  </tr>
                  <tr>
                    <td><a href="[Top_2.site]" onclick="window.location = '[Top_2.site]';" style="display: block; cursor: pointer; text-decoration: none;">[Top_2.web]</a></td>
                  </tr>
                </table>
                <hr />

 

Can anyone tell me why they are not plugging into the HTML file and how I can fix it?  I am officially pulling out my hair.  I am going to attempt to get the plug in variable in the HTML file working before I try to randomize them (baby steps).  Please help!  ??? ??? ???

Link to comment
Share on other sites

If you look at the page source does it have the content of your template "as-is," meaning does it actually have the strings: [Top_2.dealer], [Top_2.site], etc?

 

I've never used a template engine but I may be able to help you track down the source of the problem.  :D

Link to comment
Share on other sites

As being that this is actually my first attempt at creating the code for templates, I don't believe that it is "as-is".  i have been going through the code that our company uses to create the full "Dealer Locater", which was created, by hand, by the coder that preceded me.  i did not have the opportunity to learn through him his coding methods, but by seeing his code and copying the bits and pieces that made sense, I was able to create what I thought would be a working solution.

 

I was reading through the various manuals (and I mean various as in numerous) and they all say that to call the function in the HTML you use it like:

. . .snip 
<td class="titleInfo"><div align="center" class="style10">Locations
                        found close to <strong>[var.Zip]</strong>.</div></td>
                  </tr>
                  <tr>
                    <td class="titleInfo"><table width="95%" border="0" align="center" cellpadding="3" cellspacing="3">
                      <tr>
                        <td class="titleInfo">[Dealers;block=tr;serial] [Dealers_1;block=td]
                          <table width="100%" border="0" align="center" cellpadding="4" cellspacing="4">
                                        <tr>
                                          <td><span class="style13">[Dealers_1.DealerName;]</span></td>
                                        </tr>
                                        <tr>
                                          <td><table width="100%" border="0" cellspacing="1" cellpadding="1">
                                              <tr>
                                                <td class="style11">[Dealers_1.Address1]
                                                  [Dealers_1.Address2]</td>
                                              </tr>
                                              <tr>
                                                <td class="style11">[Dealers_1.City],
                                                  [Dealers_1.State] [Dealers_1.Postal]
                                                  [Dealers_1.Country]</td>
                                              </tr>
                                              <tr> . . . snip

and it works okay for what the other coder did, but I can't seem to get mine to work in the same context.  I know I'm missing something, but what?  ???

Link to comment
Share on other sites

Well,  if you're using a custom template engine written internally by your company, it would be very hard for us to tell you why it's not working just by seeing some very "high level" code.

 

Basically you're going to have to learn how to debug something on your own without any external help.  Here is the most useful tool in doing that: echo

 

Even better:

  echo "<pre>" . print_r($SomeVal, true) . "</pre>";

 

Start tossing echo statements everywhere.  Make sure the data you're receiving is the data you're expecting.  Track the programs execution with it.

 

I've seen coders with fancy IDEs, syntax high lighting, debuggers, and all sorts of other crap that couldn't figure out why their program didn't work.  99% of all my debugging is done with simple echo statements; it rarely let's me down.

Link to comment
Share on other sites

to add to that, if you want to do a major dump, use get_defined_vars():

 

echo "<pre>" . print_r(get_defined_vars(), true) . "</pre>";

 

i'd suggest exiting with an echo like that, because it's a rather lengthy array (if you're not in a low scope).

Link to comment
Share on other sites

i'll admit that i haven't really read the whole topic (i'm not well-versed in OOP at all yet), but you can put the echo wherever you want - it's just a handy tool for seeing what a variable's value is at different points in the script.  var_dump() also helps, as it gives more information than simply echoing the variable.

Link to comment
Share on other sites

echo is a PHP command so it has to be somewhere the PHP interpreter can parse it.  Since it doesn't appear your template file is working correctly, I wouldn't put it in the template file but in a .php file instead.

 

 

Link to comment
Share on other sites

Thanks.  With that help, I found out that the problem is with my class.  Apparently I am not defining the variables correctly.  In this code:

<?php

// Create a class
class BuyNow{
	var $dealer;
	var $web;
	var $site;
	var $rank;
}
// Create the Function
function BuyNow($dealer, $web, $site, $rank){

// Define the variables $dealer, $web, $site and $rank	
	$New[] = BuyNow('dealer1', 'web1', 'http://www.site1.com', '1');
	$New[] = BuyNow('dealer2', 'web2', 'http://www.site2.com', '1');
	$New[] = BuyNow('dealer3', 'web3', 'http://www.site3.com', '1');
	$New[] = BuyNow('dealer4', 'web4', 'http://www.site4.com', '1');
	$New[] = BuyNow('dealer5', 'web5', 'http://www.site5.com', '1');
	$New[] = BuyNow('dealer6', 'web6', 'http://www.site6.com', '1');
	$New[] = BuyNow('dealer7', 'web7', 'http://www.site7.com', '1');
	$New[] = BuyNow('dealer8', 'web8', 'http://www.site8.com', '1');
	$New[] = BuyNow('dealer9', 'web9', 'http://www.site9.com', '1');
	$New[] = BuyNow('dealer10', 'web10', 'http://www.site10.com', '0');
	$New[] = BuyNow('dealer11', 'web11', 'http://www.site11.com', '0');
	$New[] = BuyNow('dealer12', 'web12', 'http://www.site12.com', '0');
	$New[] = BuyNow('dealer13', 'web13', 'http://www.site13.com', '0');
	$New[] = BuyNow('dealer14', 'web14', 'http://www.site14.com', '0');
}

// Separation of dealers
if($rank = 1){
	$Top[] = $New;
}else{
	$Bottom[] = $New;
}

// Define Plug in Variables
$Top['dealer'] = $dealer;
$Top['web'] = $web;
$Top['site'] = $site;
return $Top;

$Bottom['dealer'] = $dealer;
$Bottom['web'] = $web;
$Bottom['site'] = $site;
return $Bottom;

$Tpl = new clsTinyButStrong ;

// Make Blocks 'Top' and 'Bottom'
$Tpl->MergeBlock('Top', $Top);
$Tpl->MergeBlock('Bottom', $Bottom);

$Tpl->Show();

?>

is my problem.  This is what I need to focus on.  Apparently the other files are working okay.

 

Any suggestions on what looks wrong?  I can implement the plug-ins later.

Link to comment
Share on other sites

Your problem could be this:

 

<?php
// Create a class
class BuyNow{
	var $dealer;
	var $web;
	var $site;
	var $rank;
}
// Create the Function
function BuyNow($dealer, $web, $site, $rank){

//...
?>

 

Constructors are named after the class in PHP4, so I'm guessing you intended for:

function BuyNow($dealer, $web, $site, $rank){

to define a constructor.

 

However, you have a closing curly brace after your var $rank; which effectively ends the class declaration.  This means you've create a class named BuyNow which has only four member variables and no methods or functions.

 

Here is a simple class declaration:

<?php
class BuyNow { // <-- THIS CURLY BRACE BEGINS THE CLASS
  var $var1, // <-- Some properties (or member variables)
       $var2,
       $var3;

  // Define a constructor
  function BuyNow( /* params */ ){ <-- THIS CURLY BRACE BEGINS THE CONSTRUCTOR
  } <-- THIS CURLY BRACE CLOSES THE CONSTRUCTOR

} // <-- THIS CURLY BRACE ENDS THE CLASS
?>

 

Now let's take a look at your function that you declared:

<?php
function BuyNow($dealer, $web, $site, $rank){
          // Define the variables $dealer, $web, $site and $rank	
	$New[] = BuyNow('dealer1', 'web1', 'http://www.site1.com', '1');
	$New[] = BuyNow('dealer2', 'web2', 'http://www.site2.com', '1');
	$New[] = BuyNow('dealer3', 'web3', 'http://www.site3.com', '1');
	$New[] = BuyNow('dealer4', 'web4', 'http://www.site4.com', '1');
	$New[] = BuyNow('dealer5', 'web5', 'http://www.site5.com', '1');
	$New[] = BuyNow('dealer6', 'web6', 'http://www.site6.com', '1');
	$New[] = BuyNow('dealer7', 'web7', 'http://www.site7.com', '1');
	$New[] = BuyNow('dealer8', 'web8', 'http://www.site8.com', '1');
	$New[] = BuyNow('dealer9', 'web9', 'http://www.site9.com', '1');
	$New[] = BuyNow('dealer10', 'web10', 'http://www.site10.com', '0');
	$New[] = BuyNow('dealer11', 'web11', 'http://www.site11.com', '0');
	$New[] = BuyNow('dealer12', 'web12', 'http://www.site12.com', '0');
	$New[] = BuyNow('dealer13', 'web13', 'http://www.site13.com', '0');
	$New[] = BuyNow('dealer14', 'web14', 'http://www.site14.com', '0');
}
?>

This function is next to useless.  It takes four parameters, none of which are used anywhere.  You've declared a variable in the function named $New and assigned values to it, but the variable itself is never returned.  Even if you had correctly placed this function inside the class as a constructor, it would be useless; you can tell this from the lack of the $this keyword.

 

Looking at the rest of your code, which effectively sits in the global namespace (unless this file is itself called through include):

<?php
/ Define Plug in Variables
$Top['dealer'] = $dealer; // <-- $dealer, $web, $site ALL LOOK LIKE THEY'RE
$Top['web'] = $web;      // UNDEFINED.  YOU DIDN'T SET THEM ANYWHERE
$Top['site'] = $site;       // IN THE SCRIPT YOU PASTED
return $Top;                // YOU'RE NOT IN A FUNCTION, WHAT IN THE HELL ARE YOU RETURNING
                                         // FROM?

// Technically this code won't execute since you just returned early in the above statement
$Bottom['dealer'] = $dealer; // <-- SEE THE NOT ABOVE ABOUT UNDEFINED
$Bottom['web'] = $web;      // VALUES
$Bottom['site'] = $site;
return $Bottom;  // SAME NOTE ABOUT RETURNING ABOVE REPLIES

$Tpl = new clsTinyButStrong ;

// Make Blocks 'Top' and 'Bottom'
$Tpl->MergeBlock('Top', $Top);
$Tpl->MergeBlock('Bottom', $Bottom);

$Tpl->Show();
?>

 

I suggest you read up in the PHP manual about classes as you look to be way over your head here:

http://php.net/oop

 

Notice how if you had done the debugging process with echo sooner your original post could have been: "There's something wrong with this class, please help" and you probably would have gotten a lot of useful input sooner.

 

:D

Link to comment
Share on other sites

Thank you for pointing out what is going on in my code.  I will go through the manual you suggested (I'm sure I have it here somewhere), again, and see if I can get things cleared up.  I will post here if there are more problem that arise and will post the working code for those that may need something similar in the future.

Link to comment
Share on other sites

Here we go.  I started to restructure the code so that I may define the variables inside the PHP file in itself.  Then I ran into some problems.  Using the echo statement mentioned earlier, I was able to change the code accordingly, break into the database and use it to my advantage.  Unfortunately, I got lost somewhere.  The variables are now being defined, but I can't split the ranks so that they fill two different variables, $Top and $Bottom.

 

<?php

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

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

$bnDealer['dealer'] = $dealer;
$bnDealer['web'] = $web;
$bnDealer['site'] = $site;
$bnDealer['rank'] = $rank;

return $bnDealer;
}

function ObjToArr($Object){

	foreach($Object as $Key => $Value)
		$Array[$Key] = $Value;

		return $Array;
}

$Tpl = new clsTinyButStrong ;

	$New[] = BuyNow(85, ''.$web.'/osc/', 1);
	$New[] = BuyNow(841, ''.$web.'', 1);
	$New[] = BuyNow(877, ''.$web.'/wilson.asp', 1);
	$New[] = BuyNow(778, ''.$web.'', 1);
	$New[] = BuyNow(1505, ''.$web.'', 1);
	$New[] = BuyNow(57, ''.$web.'/cellular-antennas/wilson-antennas', 1);
	$New[] = BuyNow(209, ''.$web.'/wilson_amplifier.html', 1);
	$New[] = BuyNow(1260, ''.$web.'', 1);
	$New[] = BuyNow(1532, ''.$web.'', 1);
	$New[] = BuyNow(189, ''.$web.'/wilcelananad.html', 0);
	$New[] = BuyNow(690, ''.$web.'', 0);
	$New[] = BuyNow(467, ''.$web.'', 0);
	$New[] = BuyNow(1388, ''.$web.'', 0);
	$New[] = BuyNow(1161, ''.$web.'', 0);

if($rank = 1){
	$Top = $New;
	$Tpl->MergeBlock('Top', $Top) ;
}elseif($rank = 0){
	$Bottom = $New;
	$Tpl->MergeBlock('Bottom', $Bottom) ;
}	

?>

I am also trying to place the variable $web into the $site variable so I may add extensions (for landing pages).

 

Can anyone help me figure out where I went wrong in this code?

 

The interface into the template doesn't work yet, but I am still working on that problem.  Baby steps, if you will, baby steps.

Link to comment
Share on other sites

Well this problem has been solved (sort of).  Just need to show the code:

<?php

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

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

$bnDealer['dealer'] = $dealer;
$bnDealer['web'] = $web;
$bnDealer['site'] = $site;
$bnDealer['rank'] = $rank;

return $bnDealer;
}

function ObjToArr($Object){
	foreach($Object as $Key => $Value)
		$Array[$Key] = $Value;
		return $Array;
}

$Tpl = new clsTinyButStrong ;

$New[] = BuyNow(85, 'dealerweb.com', 1);
$New[] = BuyNow(841, 'dealerweb.com', 1);
$New[] = BuyNow(877, 'dealerweb.com', 1);
$New[] = BuyNow(778, 'dealerweb.com', 1);
$New[] = BuyNow(1505, 'dealerweb.com', 1);
$New[] = BuyNow(57, 'dealerweb.com', 1);
$New[] = BuyNow(209, 'dealerweb.com', 1);
$New[] = BuyNow(1260, 'dealerweb.com', 1);
$New[] = BuyNow(1532, 'dealerweb.com', 1);
$New[] = BuyNow(189, 'dealerweb.com', 0);
$New[] = BuyNow(690, 'dealerweb.com', 0);
$New[] = BuyNow(467, 'dealerweb.com', 0);
$New[] = BuyNow(1388, 'dealerweb.com', 0);
$New[] = BuyNow(1161, 'dealerweb.com', 0);

if($rank = 1){
	$Top = $New;
	$Bottom = $New;
}elseif($rank = 0){
	$Bottom = $New;
}	

//echo "<pre>" . print_r(get_defined_vars(), true) . "</pre>";

?>

And state that this works, only to a point. For some reason my if . . . else statement is not performing the way it should.  Then again, I am not the one that "Fixed" this code.

 

I was playing around with different ways to get my variables to work.  Our company "Guru" took the code I had and worked it into what you see.

 

As for plugging it in:

 <td width="206" align="center" bgcolor="#1F70B9" class="title2"><b>BUY NOW!</b></td>
        <td width="3" class="title"></td>
        <td width="11"><img border="0" src="img/boxtitleright.jpg" width="4" height="25" /></td>
        <td rowspan="3" background="img/boxpieceright.jpg" width="10"></td>
      </tr>
      <tr>
        <td rowspan="2" colspan="2"><img border="0" src="img/dot.gif" width="14" height="5" /></td>
        <td class="contentname" align="center"><table width="100%" border="0" cellspacing="5" cellpadding="15">
          <tr>
            <td width="90%"><table width="100%" border="0" cellpadding="2" cellspacing="2">
                <tr>
                  <td><span class="style13">[var.Top.0.dealer]</span></a></td>
                </tr>
                <tr>
                  <td><a href="http://www.[var.Top.0.site]" onclick="window.location = '[var.Top.0.site]';" style="display: block; cursor: pointer; text-decoration: none;">[var.Top.0.web]</a></td>
                </tr>
              </table>
                <hr />
                <table width="100%" border="0" cellpadding="2" cellspacing="2">
                  <tr>
                    <td><span class="style13">[var.Top.1.dealer]</span></a></td>
                  </tr>
                  <tr>
                    <td><a href="http://www.[var.Top.1.site]" onclick="window.location = '[var.Top.1.site]';" style="display: block; cursor: pointer; text-decoration: none;">[var.Top.1.web]</a></td>
                  </tr>
                </table>
              <hr />
                <table width="100%" border="0" cellpadding="2" cellspacing="2">
                  <tr>
                    <td><span class="style13">[var.Top.2.dealer]</span></a></td>
                  </tr>
                  <tr>
                    <td><a href="http://www.[var.Top.2.site]" onclick="window.location = '[var.Top.2.site]';" style="display: block; cursor: pointer; text-decoration: none;">[var.Top.2.web]</a></td>
                  </tr>
                </table>
              <hr />

He changed the [Top_1.dealer] tags to say [var.Top.1.dealer] and so on through out the code.  The only problem is that I cannot get the PHP file to separate the $Top and $Bottom variables.

 

At least it plugs in properly!  Any suggestions on how to proceed from here?  ??? ???

Link to comment
Share on other sites

Fixed my little problem:

<?php

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

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

$bnDealer['dealer'] = $dealer;
$bnDealer['web'] = $web;
$bnDealer['site'] = $site;

return $bnDealer;
}

function ObjToArr($Object){
	foreach($Object as $Key => $Value)
		$Array[$Key] = $Value;
		return $Array;
}

$Tpl = new clsTinyButStrong ;

$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');

//echo "<pre>" . print_r(get_defined_vars(), true) . "</pre>";

?>

 

I basically got rid of the $rank variable, so I don't have to bother with the if...else statement.

 

Thank you all for helping, though I am sure this is not the end for this code.  I have a new problem.  Now I have to randomize the output of $Top and $Bottom.

 

Any suggestions? ???

Link to comment
Share on other sites

I'm going to guess that your problem stems from the line:

  if( $rank = 1 ){

 

Someone else had a problem constructing an if statement and I jumped in with a lengthy explanation.  Rather than retype it, here is a link to the thread:

http://www.phpfreaks.com/forums/index.php/topic,110618.0.html

 

Note the part where I talk about the difference between = and ==

Link to comment
Share on other sites

Thanks roopurt.  I am a little familiar with the shuffle command (I have used it once or twice).  I am just curious about if you could put a timer on it so that it shuffles the array(s) every . . . say . . . 2 hours?  Will the output be the same if I put the time limit on it for multiple pages, or will it shuffle it every time I return to the page?

 

Also, would I put the shuffle command after the $Top variable array and after the $Bottom variable array, or do I mix the two together in one big argument.  I've used shuffle on a single array, not two together.

 

Any comment and help are appreciated.

Link to comment
Share on other sites

PHP scripts executed through your web server (aka Apache) are executed on every request, which is to say they are executed every time someone visits the page.  Also, different people will see a different order, even if they somehow managed to make the requests at the same exact instant.

 

If you want something to run on a schedule you have to look into cron on linux servers and windows scheduler on windows servers.

 

If you're building a ranking system, which it appears you might be, you can do this in one of two approaches.

 

In terms of computational power and complexity, if it's "easy" to calculate the page rankings, you can do it in real-time on every page request.  For example, let's say your site manages a league for 100 users.  You have a page that displays users' rankings in comparison to each other.  If your chosen algorithm to calculate rankings finishes in less than (picking an arbitrary value) 2 seconds on average, then you can probably do your calculation on every page request.  This means every time someone visits the ranking page, they have to wait an extra two seconds but the rankings are always up to date.

 

On the opposite end of the spectrum, let's say your site grows to 10,000 users.  In this scenario, you discover that your ranking algorithm now takes an average of 3 minutes to execute; this is far too long to be done on every visit to the page for every user.  You now have two choices:

 

1) You can try and optimize your ranking algorithm.  Perhaps by refactoring code or changing some of the database mechanics, you can get your algorithm to finish in that acceptable two second time span.

 

2) You can use cron to schedule the ranking algorithm to execute on a timetable, like every hour, every half hour, every 8 hours, etc.  The cron job (as we call them) saves the computed rankings elsewhere in the database.  Now you modify your regular rankings page on the site to pull from the computed rankings table, which should be almost instantaneous.  The downside is now users don't have up to date rankings, but are viewing data that was calculated some time ago.

 

It is always better to try option 1 first.  I say this because in some scenarios you can get a 3 minute process down to less than a second by being clever.  However, let's pretend the best you could accomplish is to get the script from 3 minutes of execution time down to 45 seconds.  Well, 45 seconds is still too long to serve in a web browser, so you will have to set up a cron job anyways.  The difference is your cron job will execute in 45s as opposed to the original 3 minutes, and it's always a good idea to cut down on computation time (especially in busy sites).

 

Also, would I put the shuffle command after the $Top variable array and after the $Bottom variable array, or do I mix the two together in one big argument.  I've used shuffle on a single array, not two together.

I'm not sure what you mean by this.  It is only meaningful to shuffle an array that has contents so you would naturally shuffle the array after you have loaded data into it.

Link to comment
Share on other sites

I think I may have complicated the problem more than needed and retract the statement about changing at certain times.

 

I am trying to figure out how to randomize the $Top and $Bottom data, so that I get a different 1, 2, 3 etc.

<?php

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

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

$bnDealer['dealer'] = $dealer;
$bnDealer['web'] = $web;
$bnDealer['site'] = $site;

return $bnDealer;
}

function ObjToArr($Object){
	foreach($Object as $Key => $Value)
		$Array[$Key] = $Value;
		return $Array;
}

$Tpl = new clsTinyButStrong ;

$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');

?>

 

As you can see in the code the first $Top argument is automatically assigned the value 0 and same goes for the first $Bottom variable.  Being as they are separate variables ($Top and $Bottom), my question was how could I put in the shuffle command and get random dealers plugged into the other page?

 

Any ideas? Or would a random command make it easier?

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.