Solved

How can I configure dynamic behaviors for PHP?

Posted on 2007-11-17
15
213 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
  • 9
  • 6
15 Comments
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
Comment Utility
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
Comment Utility
Sure thing, do you have the php code so that i can try modify it for you?
0
 

Author Comment

by:gmahler5th
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
Comment Utility

<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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
cool glad it works for you and glad to help :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now