Trouble retreiving string from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Trouble retreiving string from array

Post by bwv2 »

I've found myself in a bit of a crux. I have a product database storing brands and models. In this part of the code, the user is given a list of brands that they may or may not carry in their store. They check the boxes next to all of their brands, and then the next page presents them with all of the models from each of the brands they selected.

I'm having a problem because if they select a brand with more than one word in the name, it doesn't store both words in the array used to query mySQL. Since only one word is stored, mySQL does not send back a result for that brand.

Below is the code for this (I cut it short after the problem area). I think the problem is in lines 5 and 6. Does anyone know how to store the complete string in an array and have it retreivable as a string later? I'm going bald here, any help would be appreciated.

Code: Select all

//-----CHOOSE ALL BRANDS FROM TABLE------//
	$sql="SELECT DISTINCT manu FROM panels";
	$result=mysql_query($sql, $conn)or die("Could not Query1: " . mysql_error());
//-----LOAD BRANDS INTO AN ARRAY--------//
	while($panelResult=mysql_fetch_array($result)){
			$panelArray[]=$panelResult[0];
	}
//-----PULL PRODUCTS FROM EACH CHOSEN BRAND IN ARRAY--------//
	foreach($panelArray as $panel){........
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try echoing out $panelResult[0] to see if you have multiword phrases in your db. Your $panelArray should be getting set properly with the code you have there, although I would declare it above your loop.

ie:

Code: Select all

$panelArray = array();
while(...)
{
   echo panelResult[0];
   $panelArray[] = $panelResult[0];
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

If I understand the problem enough you can use concatenation -- $row[0].$row[2] -- or you could use a multidimensional array where $panelArray[0] = array($row[0], $row[1]) etc ;)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

my guess is that key 0 is not what he thinks it is. Which is why I suggested that he echo it out.

Really he should use an associative array and use the db field name for the key name to ensure he's getting exactly what he needs / wants...
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Post by bwv2 »

Thanks for the replies. I've made some progress and have narrowed it down a bit. I did what Burrito said and echoed it out, and it showed that my array did contain the full brand names. However, I went 2 lines down and printed out the $_POST[$panel'] for each brand that was selected. This is where the problem is.

Say the user selected the checkbox for the brand "BrandX" on the previous page. He submits the page, and then the next page tries to call up this $_POST variable to print to the screen. If "BrandX" is a single word title, it prints the title. if "Brand X" is a 2 word title, it prints nothing.

This leads me to believe that I need some way of transferring strings through $_POST. It works for single word strings, but not multi word strings. Any ideas?

Thanks so much for your help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

are you passing your post vars from an array?

ie:

Code: Select all

<input type="checkbox" name="someArr[]" value="some string">
<input type="checkbox" name="someArr[]" value="some other string">
<input type="checkbox" name="someArr[]" value="some third string">
maybe show some more code....
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Post by bwv2 »

Okay, here's the whole shebang. I wrote 2 new pages to isolate the problem. I have copied each of these pages below.

The first is the input page. It pulls out all of the manufacturers from the product database, then prints them to the screen with checkboxes next to each. As the user, I checked all boxes before proceeding to page 2. The names I checked included both single word names and multi-word names. Here's page 1:

Code: Select all

<html>
<body>
<?
$conn = mysql_connect($_SESSION['host'], $_SESSION['adminUser'], $_SESSION['adminPass']);
$db = mysql_select_db($_SESSION['dbPreface']."renewabledb", $conn);
?>
<form action="test2.php" method="post">
<table>
			<?
			$sql="SELECT DISTINCT manu FROM panels";
			$result=mysql_query($sql, $conn)or die("Could not Query: " . mysql_error());

			while($panelResult=mysql_fetch_array($result)){
				$panelArray[]=$panelResult[0];
			}
			foreach($panelArray as $panel){ 
				echo "<tr><td></td><td><INPUT TYPE=CHECKBOX NAME=\"$panel\" VALUE= \"$panel\">$panel</td></tr>\n"; 
				}
			?>
			
</table>
<input type="submit" value="Next ->" name="submit">
	
</form>
</body>
</html>
Now the first page sends the info to page 2, which pulls out two types of info. The first is the actual name as it appears in the database. The 2nd is the name as it was retrieved through $_POST. It takes these two names and prints them to the screen, one above the other. Here's that page:

Code: Select all

<html>
<body>
<?
$conn = mysql_connect($_SESSION['host'], $_SESSION['adminUser'], $_SESSION['adminPass']);
$db = mysql_select_db($_SESSION['dbPreface']."renewabledb", $conn);
?>
<table>
			<?
			$sql="SELECT DISTINCT manu FROM panels";
			$result=mysql_query($sql, $conn);
			
			while($panelResult=mysql_fetch_array($result)){
					$panelArray[]=$panelResult[0];
			}
			foreach($panelArray as $panel){
				print "panelACTUAL: ".$panel."<br>";
				print "panelPOST: ".$_POST[$panel]."<br><br>";
			}
			?>
</table>
	
</form>
</body>
</html>
The result? I have copied the actual printing to the screen below (with manufacturer names altered to protect the innocent):
__________________________
panelACTUAL: Big Truck
panelPOST:

panelACTUAL: Evergreen Company
panelPOST:

panelACTUAL: Ciaody
panelPOST: Ciaody

panelACTUAL: Goody
panelPOST: Goody

panelACTUAL: The News Company
panelPOST:

panelACTUAL: Sharp
panelPOST: Sharp

panelACTUAL: Townhouse
panelPOST: Townhouse
_______________________________

As you can see, only the single word names printed, whereas the multi-word ones did not. If it makes any difference, the names are stored in the mySQL table as VARCHAR(30). Thanks for the continuing help, I look forward to seeing what I'm doing wrong. Thanks.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

for all intents and purposes...that should work. are there any quotes in the panel names / values?

try viewing the source on the form page and see if the checkbox names / values are populated as you expect.

you might also trying print_r()'ing the $_POST[] array to see what it shows (on the action page).
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

on second thought, I think php will replace the spaces in the post var names with underscores (_).

you will need to do a str_replace() to replace the spaces in your $panel var with underscores...then it should work.

try this:

Code: Select all

$npost = str_replace(" ","_",$panel);
print "panelPOST: ".$_POST[$npost]."<br><br>";
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Post by bwv2 »

I love you man. Beautiful....
Post Reply