Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

How can I configure dynamic behaviors for PHP?

I am trying to reduce the amount of coding and semi-automate the image display and order code in PHP.  For example, the following page  http://tinyurl.com/38lk89  I'd like the code to display the image and Add to Card code automatically created using PHP based on some config parameters that I have specified in a properties file.   E.g. if I need 20 images displayed on the page, the code to display 20 images is created on-the-fly when the page is being loaded.   I've done this using Cold Fusion, but those servers are so expensive.  Would love to be able to do the same in PHP.

Thanks.
0
gmahler5th
Asked:
gmahler5th
  • 9
  • 6
1 Solution
 
nizsmoDeveloperCommented:
you can pass in the number of results (images) you want displayed via url:

yourphpfile.php?num=20

then get the num variable:

$num = $_REQUEST["num"];

and modify your number of images displayed accordingly. ie if it is coming out of your database, then:

$query = "SELECT * FROM your_table ...etc.... LIMIT 0, $num";

then query your database, it would only return 20 results.

Hope this helps in some way.
0
 
gmahler5thAuthor Commented:
I'm not using a database, and would prefer not to if possible.

I need the properties file for each "collection" to dynamically build the <img> tag to source the photo name, and also dynamically build the PayPal order code.  

This should all be possible using text files right???
0
 
gmahler5thAuthor Commented:
I should be able to set the following variables

pageName=plane.php
projectName=From The Air II
numOfImages=x
imageSrc=/images/somelocation/imgbasename_<xx>.jpg
<Insert PayPal order code here with imageID inserted automatically>


And loop over this code x number of times to build my page.

That's what I want to do, without using a database.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nizsmoDeveloperCommented:
yes you can use a for loop or a while loop to echo out your <img> code and your paypal code.

Didn't know you weren't using a database,  but it is the same concept.

Let me know how you go.
0
 
gmahler5thAuthor Commented:
Nizsmo, I'm posting the question because I'm not sure exactly of the PHP syntax to write this code.  Can you please help me with that?

I know it's possible!  >8-)
0
 
nizsmoDeveloperCommented:
Sure thing, do you have the php code so that i can try modify it for you?
0
 
gmahler5thAuthor Commented:
Basically starting from scratch, so...
<html>
<body><?php 
$i=1;
while($i<=20)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }
?></body>
</html>

Open in new window

0
 
nizsmoDeveloperCommented:
ok but you have the code for:
http://www.toutphotography.com/plane.php
?

then we can modify this using the for loop.
0
 
gmahler5thAuthor Commented:

<img src="<urlStub>-<imgID>.jpg" alt="<collection name> <imgID>" width="850" height="638" /> <br />
                   <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
              <? include("somepricelist.php") ?>
              <input name="submit" type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="Make payments with PayPal - it's fast, free and secure!" align="top" border="0">
              <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
              <input type="hidden" name="add" value="1">
              <input type="hidden" name="cmd" value="_cart">
              <input type="hidden" name="business" value="some email address">
              <input type="hidden" name="item_name" value="<collection name> <imgID>">
              <input type="hidden" name="item_number" value="imgID">
              <input type="hidden" name="no_note" value="1">
              <input type="hidden" name="currency_code" value="USD">
              <input type="hidden" name="bn" value="PP-ShopCartBF">
            </form>
            <br />
            <br />

Open in new window

0
 
gmahler5thAuthor Commented:
I have this code working but.... I can't get it to loop
<?php 
$count = "20";
$imgSrc = "plane/plane-";
?>
<img src="<?=$imgSrc?><?=$count?>.jpg">

Open in new window

0
 
gmahler5thAuthor Commented:
I think using readdir or scandir would be easier, so I it would read all .jpg files and sort them by name, and I would not have to configure the file each time.  If anyone can provide that, I'd prefer this design.
0
 
nizsmoDeveloperCommented:
Below is the code that may possibily work. However you need to change most importantly (at the start) the image directory that you are working with relative to your script's location.

Also you have to change the form details, as they are (i suspect) not correct, and I do not know your details for that.

I have left comments where appropriate.
<?php
$max = 20; // how many pictures to show
 
$image_file_path = 'images/'; // Directory of your Images
$regpattern = '.jpg$|.jpeg$|.png$|.tif$|.gif$'; // Extensions allowed
$d = dir($image_file_path) or die("Wrong path: $image_file_path");
 
$counter = 0;
while (false !== ($entry = $d->read()) && $counter <= $max) 
{
	if($entry != '.' && $entry != '..' && !is_dir($dir.$entry) && ereg($regpattern,$entry))
	{
		$Images[] = $entry;
		$counter++;
	}
}
$d->close();
$i=0;
 
while($i<count($Images))
{
	echo "<div><img src=\"" . $image_file_path . $Images[$i] . "\" width=\"850\" height=\"638\" />";
	echo "<form target=\"paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">";
	include("somepricelist.php");
	echo "<input name=\"submit\" type=\"image\" src=\"https://www.paypal.com/en_US/i/btn/x-click-but22.gif\" alt=\"Make payments with PayPal - it's fast, free and secure!\" align=\"top\" border=\"0\">
              <img alt=\"\" border=\"0\" src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" width=\"1\" height=\"1\">
              <input type=\"hidden\" name=\"add\" value=\"1\">
              <input type=\"hidden\" name=\"cmd\" value=\"_cart\">
              <input type=\"hidden\" name=\"business\" value=\"some email address\">
              <input type=\"hidden\" name=\"item_name\" value=\"<collection name> <imgID>\">
              <input type=\"hidden\" name=\"item_number\" value=\"imgID\">
              <input type=\"hidden\" name=\"no_note\" value=\"1\">
              <input type=\"hidden\" name=\"currency_code\" value=\"USD\">
              <input type=\"hidden\" name=\"bn\" value=\"PP-ShopCartBF\">
            </form>
		";
	echo "</div>";
	$i++;
	// YOU HAVE TO REPLACE THE ABOVE ECHO <FORM> CODE DETAILS WITH YOUR DETAILS AS I AM UNFAMILIAR
	// WITH YOUR <collection name> etc...
}
 
?>

Open in new window

0
 
gmahler5thAuthor Commented:
Sweet, that works!  I got something similar working on my own but without the PayPal code.  Since <collection name> and <imgID> are going to be dynamic values, how do use the PHP script to add those?  After looking at this code I'm still trying to figure out the rules of syntax, which I'm sure I will pick up in time.

Also, I'm trying to figure out how to sort or order results in ASC by name.  
<input type=\"hidden\" name=\"item_name\" value=\"<collection name> <imgID>\">
<input type=\"hidden\" name=\"item_number\" value=\"imgID\">

Open in new window

0
 
gmahler5thAuthor Commented:
Nevermind I got the hidden fields to display the name properly.

I just need to add the sort by name ASC.

Thanks.
0
 
nizsmoDeveloperCommented:
cool glad it works for you and glad to help :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now