Jump to content

Wishlist Generator Problem


sweinkauf13

Recommended Posts

Ok. Well i'm designing a wishlist generator and its giving me a weird result when it generates the final list. Here our the codes for the entree interface and the your list script.

<?#########################Create Entree Interface##########################write out top of entree table$form_block = "<form method=\"post\" action=\"your_wishlist.php\"><input type=\"hidden\" value=\"$_POST[wish_list_name]\"><table cellspacing=5 cellpadding=5><tr><th>Item Name</th><th>Item Price</th><th>Item Location</th><th>Rating</th></tr>";#count from 0 until you reach specified number of entree itemsfor($i = 0; $i <$_POST[item_num]; $i++){#add one entree row$form_block .= "<tr><td align=\"center\"><input type=\"text\" name=\"item_name[$i]\" size=\"25\"></td><td align=\"center\"><input type=\"text\" name=\"item_price[$i]\" size=\"15\"></td><td align=\"center\"><input type=\"text\" name=\"item_location[$i]\" size=\"25\"></td><td align=\"center\">	<select name=\"item_rating[$i]\"><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"9\">9</option><option value=\"10\">10</option>	</select></td></tr>";}##now write in the end of the form block#$form_block .= "<TR><TD ALIGN=CENTER COLSPAN=4><INPUT TYPE=\"submit\" VALUE=\"Create Wishlist\"></TD></TR></TABLE></form>";#now write it out to the page?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Wish List Generator</title><link rel=stylesheet type="text/css" href="style.css"></head><body><H1><p>Add Items To <? echo "$_POST[wish_list_name]"; ?></p></H1><? echo "$form_block"; ?></BODY></HTML>

<?#########################Create WishList Print-Out##########################Build begginging of table$wish_list = "<table cellspacing=5 cellpadding=5><tr><th>Item Name</th><th>Item Price</th><th>Item Location</th><th>Rating</th></tr>";for ($i = 0; $i < count($_POST[item_name]); $i++) {#add the data to the wishlist one row at a time$wish_list .= "<tr><td align=\"center\">$_POST[item_name][$i]</td><td align=\"center\">$_POST[item_price][$i]</td><td align=\"center\">$_POST[item_location][$i]</td><td align=\"center\">$_POST[rating][$i]</td></tr>";}##write this out to the screen?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Wish List Generator</title><link rel=stylesheet type="text/css" href="style.css"></head><body><H1><p><center><? echo "$_POST[wish_list_name]"; ?></p></H1><? echo "$wish_list"; ?></BODY></HTML>

Now what i get is the table but where the data should be it reads Array[0] and so on. Please help me fix this problem.thanks in advance,youngwebmaster

Link to comment
Share on other sites

Try this function to print out the $_POST array:

// array_debug - print out a formatted list of an arrayfunction array_debug($array_name) { 		  echo '<pre>'; 		  ksort($array_name); 		  print_r($array_name); 		  echo '</pre>'; 		} // end func

Call the function using the array name used in your script.

Link to comment
Share on other sites

I tried what you said...

// array_debug - print out a formatted list of an arrayfunction array_debug($array_name) { echo '<pre>'; ksort($array_name); print_r($array_name); echo '</pre>'; } // end func
and now instead of getting a Array[0] and so on, i get that chunk of code in each place that should be the information. Anything else i can try?
Link to comment
Share on other sites

ya nevermind. It did the same thing...

I tried what you said...
// array_debug - print out a formatted list of an arrayfunction array_debug($array_name) { echo '<pre>'; ksort($array_name); print_r($array_name); echo '</pre>'; } // end func
and now instead of getting a Array[0] and so on, i get that chunk of code in each place that should be the information. Anything else i can try?
Link to comment
Share on other sites

Yes i did. Here is how your_list.php looks now after i inserted your code block. But one thing. I After your code block didn't work, i tried taking the <pre> stuff out. So this script is puposfully missing that. I don't know if that's wrong or right.your_list.php

<?#########################Create WishList Print-Out##########################Build begginging of table$wish_list = "<table cellspacing=5 cellpadding=5><tr><th>Item Name</th><th>Item Price</th><th>Item Location</th><th>Rating</th></tr>";for ($i = 0; $i < count($_POST[item_name]); $i++) {#add the data to the wishlist one row at a time$wish_list .= "<tr><td>// array_debug - print out a formatted list of an arrayfunction array_debug($_POST[$item_name]) { 		  ksort($_POST[item_name]); 		  print_r($_POST[item_name]); 		} // end func</td><td>// array_debug - print out a formatted list of an arrayfunction array_debug($_POST[item_price]) { 		  ksort($_POST[item_price]); 		  print_r($_POST[item_price]); </td><td>// array_debug - print out a formatted list of an arrayfunction array_debug($_POST[item_location]) { 		  ksort($_POST[item_location]); 		  print_r($_POST[item_location]); </td><td>// array_debug - print out a formatted list of an arrayfunction array_debug($_POST[item_rating]) { 		  ksort($_POST[item_rating]); 		  print_r($_POST[item_rating]); 		} // end func</td></tr>";}##write this out to the screen?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Wish List Generator</title><link rel=stylesheet type="text/css" href="style.css"></head><body><H1><p><center><? echo "$_POST[wish_list_name]"; ?></p></H1><? echo "$wish_list"; ?></BODY></HTML>

And if this is helpful...entree_interface.php (Note: this script currently works to my knowlage)

<?#########################Create Entree Interface##########################write out top of entree table$form_block = "<form method=\"post\" action=\"your_wishlist.php\"><input type=\"hidden\" value=\"$_POST[wish_list_name]\"><table cellspacing=5 cellpadding=5><tr><th>Item Name</th><th>Item Price</th><th>Item Location</th><th>Rating</th></tr>";#count from 0 until you reach specified number of entree itemsfor($i = 0; $i <$_POST[item_num]; $i++){#add one entree row$form_block .= "<tr><td align=\"center\"><input type=\"text\" name=\"item_name[]\" size=\"25\"></td><td align=\"center\"><input type=\"text\" name=\"item_price[]\" size=\"15\"></td><td align=\"center\"><input type=\"text\" name=\"item_location[]\" size=\"25\"></td><td align=\"center\">	<select name=\"item_rating[]\"><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"9\">9</option><option value=\"10\">10</option>	</select></td></tr>";}##now write in the end of the form block#$form_block .= "<TR><TD ALIGN=CENTER COLSPAN=4><INPUT TYPE=\"submit\" VALUE=\"Create Wishlist\"></TD></TR></TABLE></form>";#now write it out to the page?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Wish List Generator</title><link rel=stylesheet type="text/css" href="style.css"></head><body><H1><p>Add Items To <? echo "$_POST[wish_list_name]"; ?></p></H1><? echo "$form_block"; ?></BODY></HTML>

Did i put it in the right place? What do i need to do differently to make this work?

Link to comment
Share on other sites

What you've not realized is that the code given to you defines a function, you need only to call it.Instead of:

<?phpfunction array_debug($_POST[$item_name]) { 		  ksort($_POST[item_name]); 		  print_r($_POST[item_name]); 		}function array_debug($_POST[item_price]) { 		  ksort($_POST[item_price]); 		  print_r($_POST[item_price]);  }?>

Use:

<?phpfunction array_debug($array_name) { 		  echo '<pre>'; 		  ksort($array_name); 		  print_r($array_name); 		  echo '</pre>'; 		} //This defines the function.array_debug($_POST[item_name]); // This calls the function.?>

Link to comment
Share on other sites

ok. I did what you said and got this. The following is the source code for the page it generated so you can see what is happening:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Wish List Generator</title><link rel=stylesheet type="text/css" href="style.css"></head><body><H1><p><center></p></H1><table cellspacing=5 cellpadding=5><tr><th>Item Name</th><th>Item Price</th><th>Item Location</th><th>Rating</th></tr><tr><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td></tr><tr><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td></tr><tr><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td><td>function array_debug() { 		  echo '<pre>'; 		  ksort(); 		  print_r(); 		  echo '</pre>'; 		} //This defines the function.array_debug(Array); // This calls the function.</td></tr></BODY></HTML>

Now here is my source code for your_list.php after i inserted the recomended block.

<?#########################Create WishList Print-Out##########################Build begginging of table$wish_list = "<table cellspacing=5 cellpadding=5><tr><th>Item Name</th><th>Item Price</th><th>Item Location</th><th>Rating</th></tr>";for ($i = 0; $i < count($_POST[item_name]); $i++) {#add the data to the wishlist one row at a time$wish_list .= "<tr><td>function array_debug($array_name) { 		  echo '<pre>'; 		  ksort($array_name); 		  print_r($array_name); 		  echo '</pre>'; 		} //This defines the function.array_debug($_POST[item_name]); // This calls the function.</td><td>function array_debug($array_name) { 		  echo '<pre>'; 		  ksort($array_name); 		  print_r($array_name); 		  echo '</pre>'; 		} //This defines the function.array_debug($_POST[item_price]); // This calls the function.</td><td>function array_debug($array_name) { 		  echo '<pre>'; 		  ksort($array_name); 		  print_r($array_name); 		  echo '</pre>'; 		} //This defines the function.array_debug($_POST[item_location]); // This calls the function.</td><td>function array_debug($array_name) { 		  echo '<pre>'; 		  ksort($array_name); 		  print_r($array_name); 		  echo '</pre>'; 		} //This defines the function.array_debug($_POST[item_rating]); // This calls the function.</td></tr>";}##write this out to the screen?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Wish List Generator</title><link rel=stylesheet type="text/css" href="style.css"></head><body><H1><p><center><? echo "$_POST[wish_list_name]"; ?></p></H1><? echo "$wish_list"; ?></BODY></HTML>

I wish i could debug this myself but your getting into stuff i'm not very familar with. Sorry i can't be more help. So if there is anything i can do, please let me know.Thanks again,youngwebmaster

Link to comment
Share on other sites

You misunderstand. You must define the function before you do anything. The 'function ' statement doesn't do anything by itself. And putting it inside of a string will do even less. Place it like so:

<?function array_debug($array_name) { 		  echo '<pre>'; 		  ksort($array_name); 		  print_r($array_name); 		  echo '</pre>'; 		} //This defines the function.array_debug($_POST);?>

Then tell us what appears on the screen.EDIT: I just tried a simplified version on my server and it works fine:testforms.php

<html><body><form method="post" action="testforms2.php"><table>	<tr>		<th>Name</th>		<th>Price</th>		<th>Rating</th>	</tr>	<?php	for ($i = 0; $i < 5; $i++)	{		?>	<tr>		<td><input type="text" name="item_name[<?=$i?>]" /></td>		<td>$<input type="text" name="item_price[<?=$i?>]" /></td>		<td><select size="1" name="item_rating[<?=$i?>]">				<option>1</option>				<option>2</option>				<option>3</option>				<option>4</option>				<option>5</option>		</select></td>	</tr>		<?php	}	?>	<tr>		<td colspan="2"> </td>		<td><input type="submit" value="Test" /></td>	</tr></table></form></body></html>

testforms2.php

<html><body><pre><?php print_r($_POST); ?></pre><p> </p><p> </p><table>	<tr>		<th>Name</th>		<th>Price</th>		<th>Rating</th>	</tr>	<?php		for ($i = 0; $i < count($_POST[item_name]); $i++)		{	?>	<tr>		<td><?=$_POST[item_name][$i]?></td>		<td><?=$_POST[item_price][$i]?></td>		<td><?=$_POST[item_rating][$i]?></td>	</tr>	<?php		}	?></table></body></html>

HTML Output of testforms2.php

<html><body><pre>Array(	[item_name] => Array		(			[0] => 1			[1] => 2			[2] => 3			[3] => 4			[4] => 5		)	[item_price] => Array		(			[0] => 1			[1] => 2			[2] => 3			[3] => 4			[4] => 5		)	[item_rating] => Array		(			[0] => 1			[1] => 2			[2] => 3			[3] => 4			[4] => 5		))</pre><p> </p><p> </p><table>	<tr>		<th>Name</th>		<th>Price</th>		<th>Rating</th>	</tr>		<tr>		<td>1</td>		<td>1</td>		<td>1</td>	</tr>		<tr>		<td>2</td>		<td>2</td>		<td>2</td>	</tr>		<tr>		<td>3</td>		<td>3</td>		<td>3</td>	</tr>		<tr>		<td>4</td>		<td>4</td>		<td>4</td>	</tr>		<tr>		<td>5</td>		<td>5</td>		<td>5</td>	</tr>	</table></body></html>

Feel free to try it yourself.

Link to comment
Share on other sites

That function is designed to output an entire Array.Example:

array_debug($_SERVER);

Use it is confirm the contents of the POST or SERVER arrays to see what information is being captured by the request as a Debugging tool. Remove it from the production copy of the Code.

Link to comment
Share on other sites

When you do something like this:

$wish_list .= "<tr><td align=\"center\">$_POST[item_name][$i]</td><td align=\"center\">$_POST[item_price][$i]</td><td align=\"center\">$_POST[item_location][$i]</td><td align=\"center\">$_POST[rating][$i]</td></tr>";}

You are confusing PHP. Or, more specifically, you're being ambiguous and PHP is choosing what it thinks is correct. When PHP sees this:<td align=\"center\">$_POST[rating][$i]</td>It parses it like this:<td align=\"center\">{$_POST[rating]}[{$i}]</td>and converts it to this:<td align="center">Array[0]</td>In other words, it doesn't know whether you want to print $_POST['rating'], or $_POST['rating'][$i]. In this case, it prints $_POST['rating'] (which is an array, hence the word "Array"), and then $i. This is why you need to use brackets to tell it what you mean when you embed variables in strings. Also use quotes in the array key.

$wish_list .= "<tr><td align=\"center\">{$_POST['item_name'][$i]}</td><td align=\"center\">{$_POST['item_price'][$i]}</td><td align=\"center\">{$_POST['item_location'][$i]}</td><td align=\"center\">{$_POST['rating'][$i]}</td></tr>";}

When PHP sees this:<td align=\"center\">{$_POST['rating'][$i]}</td>It's obvious what you're asking it to do, there's no more ambiguity.

Link to comment
Share on other sites

thank you justsomeguy. I understand this now and thanks to all who have helped me this far. I'll try what you said and post back.thanks,youngwebmaster

Link to comment
Share on other sites

Alright. It's all fixed. Thank you to everyone who has taken thier time to help me solve my problem. I really appreciate it.-youngwebmaster

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...