Jump to content

array extract


AL123

Recommended Posts

I am new to coding and I am trying to write a simple business registration app.

a foreach loop populates a select list and a nested loop checks to see if something new is being added.

If so write to the DB. Great.

Only part of it is working, I am getting the correct businessId but the categoryId is an array:

 

        188 Array

189 Array

190 Array

191 Array

192 Array

195 Array

196 Array

 

Here is the code:


<form method="post" action="?">
<table>
	<tr><td class="picklist"><?php echo $pickMessage; ?>
	<select name="bizCatSelect[]" size="4" multiple>
	<?php				
		$sql = "SELECT categoryId FROM categories";
		$sth = $dbh->prepare($sql);
		$sth-> execute();
		$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);

		foreach($result as $value)
		{
			if($addRecord == 1)
			{
				$selected = false;
				if(in_array($value, $bizCatSelect)) // $row[1]
				{

					$sql = "INSERT INTO businessCat(businessId,categoryId) VALUES(:bizId, :bizCatSelect)";
					$sth = $dbh->prepare($sql);
					$sth->bindValue(':bizId', $bizId, PDO::PARAM_STR);
					$sth->bindValue(':bizCatSelect', $bizCatSelect, PDO::PARAM_STR);
					$query = $sth->execute();

					$params = array($bizId, $bizCatSelect);  // $row[0] $value
					print_r($params);
					//$resp = $sth->execute($query, $params);

					echo "<option selected=\"$value\">$value</option>\n"; // $row[1]
					$selected = true;
				}
				if($selected == false){echo "<option value=\"$value\">$value</option>\n";}
			}

			else{ echo "<option value=\"$value\">$value</option>\n";}
			//echo "<option value=\"$value\">$value</option>\n";
		}
	?>
	</select>
	</td>
	<td class="addlist">
		<table>
		<tr><td class="formLable">Business Name:</td>
			<td><input type="text" name="bizName" size="40" maxlength="255" value="<?php echo $bizName; ?>"></td>
		</tr>
		<tr><td class="formLable">Address:</td>
			<td><input type="text" name="bizAddress" size="40" maxlength="255" value="<?php echo $bizAddress; ?>" ></td>
		</tr>
		<tr><td class="formLable">City:</td>
			<td><input type="text" name="bizCity" size="40" maxlength="128" value="<?php echo $bizCity; ?>" ></td>
		</tr>
		<tr><td class="formLable">Telephone:</td>
			<td><input type="text" name="bizTele" size="40" maxlength="64" value="<?php echo $bizTele; ?>" ></td>
		</tr>
		<tr><td class="formLable">URL:</td>
			<td><input type="text" name="bizUrl" size="40" maxlength="255" value="<?php echo $bizUrl; ?>" ></td>
		</tr>
		</table>
	</td>
	</tr>
</table>

<p><input type="hidden" name="addRecord" value="1">
<?php
if($addRecord == 1)
{echo "<p><a href=\"?\">Add another business</a></p>";}
else
{echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Add Business\">";}
?>
</p>

</form>

 

I just can't seem to get this last part, any suggestions?

AL

 

Link to comment
Share on other sites

I changed every thing to $value[0] and only the first letter of my array is chosen.

EX:

 echo "<option value=\"$value[0]\">$value[0]</option>\n";} 

 

I also tryed this to no avail.

 if(in_array($value[0], $bizCatSelect)) 

 

And both together.

 

Do I need to do 2 insert statements or maybe run another loop on $value???

 

AL

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.