Advertisement

10.11.2008 at 05:14PM PDT, ID: 23807124
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.4

PHP Form Array, Help Optimizing Function

Asked by gwkg in PHP Scripting Language

I need help optimizing this code, if possible.

I have a form that allows 3 urls and each url has 3 text fields

price, store, url

So the final url will be

$29 @ Store.com (where store.com with be an link to the store)

Once the form is submitted the data is in the post variables

$_POST['c_price1'], $_POST['c_store1'],$_POST['c_url1']
$_POST['c_price2'], $_POST['c_store2'],$_POST['c_url2']
$_POST['c_price3'], $_POST['c_store3'],$_POST['c_url3']

There are 3 parts to the code snippet.

1. The form that captures the input
2. The method that creates an array from the input
3. The html output

I skipped the functions that parse and check the $_POST variables.  The code posted works fine, I just want to know if there is a way I can shorten/optimize any of it.

Is there a better way to structure the form, other than c_store1, c_store2, etc.?  Will it create an array if leave off the 1,2,3 or will each subsequent variable overwrite the last?

Is there a better way to write the shoparound() function?  My main problem right now is if I want to change the number of urls I have to go back and manually add more if / else statements.   There has to be a better way to write this.

I also cannot allow the number of URLs to be dynamically determined by the form input.  Eventually I would like to add a check box that adds another form input when checked, but as its written now, the number of URLs are limited to the number of if / else statements in the shoparound() function.

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
// form input
    <fieldset class="block">
    	<legend>Shop Around</legend>
        <table>
        <?php for($i=1;$i<=3;$i++) { ?>
        <tr>
        <td><label for="c_price<?php echo $i; ?>"><span>Price</span>
        	<input type="text" name="c_price<?php echo $i; ?>" id="c_price<?php echo $i; ?>" size="8" value="<?php echo $_POST['c_price'.$i]; ?>" />
        </label></td>
        <td><label for="c_store<?php echo $i; ?>"><span>Store</span>
        	<input type="text" name="c_store<?php echo $i; ?>" id="c_store<?php echo $i; ?>" size="15" value="<?php echo $_POST['c_store'.$i]; ?>" />
        </label></td>
        <td><label for="c_url<?php echo $i; ?>"><span>URL</span>
        	<input type="text" name="c_url<?php echo $i; ?>" id="c_url<?php echo $i; ?>" size="40" value="<?php echo $_POST['c_url'.$i]; ?>" />
        </label></td>
        </tr>
        <?php } ?>
        </table>
    </fieldset>
 
// method in ebay class
public function shoparound() {
	if (!empty($this->c_url1))
		$result[] = array("price" => $this->c_price1,"store" => $this->c_store1,"url" => $this->c_url1);
	if (!empty($this->c_url2))
		$result[] = array("price" => $this->c_price2,"store" => $this->c_store2,"url" => $this->c_url2);
	if (!empty($this->c_url3))
		$result[] = array("price" => $this->c_price3,"store" => $this->c_store3,"url" => $this->c_url3);
			
	return $result;
	}
 
 
 
// html output
<ul>
<?php $compare = $ebay->shoparound();
	if(is_array($compare)) { 
	foreach($compare as $c) { ?>
 <li>$<?php echo $c['price']; ?> @ <a href="<?php echo $c['url']; ?>"><?php echo $c['store']; ?></a></li>
<?php } } else { ?>
<li>I cannot find this classic style online.</li>
<?php } ?>
</ul>
 
Loading Advertisement...
 
[+][-]10.11.2008 at 07:14PM PDT, ID: 22696000

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: PHP Scripting Language
Sign Up Now!
Solution Provided By: Hube02
Participating Experts: 2
Solution Grade: A
 
 
[+][-]10.12.2008 at 02:39AM PDT, ID: 22696819

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10.12.2008 at 02:47AM PDT, ID: 22696833

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.12.2008 at 08:37AM PDT, ID: 22697765

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.12.2008 at 01:44PM PDT, ID: 22698791

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628