Solved

How can I configure dynamic behaviors for PHP?

Posted on 2007-11-17
15
253 Views
Last Modified: 2008-02-01
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
Comment
Question by:gmahler5th
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20306240
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
 

Author Comment

by:gmahler5th
ID: 20306248
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
 

Author Comment

by:gmahler5th
ID: 20306256
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 21

Expert Comment

by:nizsmo
ID: 20306262
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
 

Author Comment

by:gmahler5th
ID: 20306273
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20306297
Sure thing, do you have the php code so that i can try modify it for you?
0
 

Author Comment

by:gmahler5th
ID: 20306303
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20306337
ok but you have the code for:
http://www.toutphotography.com/plane.php
?

then we can modify this using the for loop.
0
 

Author Comment

by:gmahler5th
ID: 20306363

<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
 

Author Comment

by:gmahler5th
ID: 20306487
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
 

Author Comment

by:gmahler5th
ID: 20306623
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
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20306657
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
 

Author Comment

by:gmahler5th
ID: 20307931
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
 

Author Comment

by:gmahler5th
ID: 20307967
Nevermind I got the hidden fields to display the name properly.

I just need to add the sort by name ASC.

Thanks.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20308717
cool glad it works for you and glad to help :)
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question