Jump to content

[SOLVED] Can you help me with inserting and retrieving arrays?


JPark

Recommended Posts

I am trying to build a website where customers can go to request reports for various economic sectors (mining, wholesale trade, retail trade, etc.).  Each sector has between 5 - 50 reports and so a portion of the form looks like

...<tr>
<td><input type='checkbox' name='21[]' value='Oil and gas extraction (2111)' />2111 Oil and gas extraction</td>
<td><input type='checkbox' name='21[]' value='Crude petroleum and natural gas extraction (211111)' />211111 Crude petroleum and natural gas extraction</td>
<td><input type='checkbox' name='21[]' value='Natural gas liquid extraction (211112)' />211112 Natural gas liquid extraction</td>
<td><input type='checkbox' name='21[]' value='Coal mining (2121)' />2121 Coal mining</td>
<td><input type='checkbox' name='21[]' value='Bituminous coal and lignite surface mining (212111)' />212111 Bituminous coal and lignite surface mining</td>
</tr>
<tr>
<td><input type='checkbox' name='21[]' value='Bituminous coal underground mining (212112)' />212112 Bituminous coal underground mining</td>
<td><input type='checkbox' name='21[]' value='Anthracite mining (212113)' />212113 Anthracite mining</td>
<td><input type='checkbox' name='21[]' value='Metal ore mining (2122)' />2122 Metal ore mining</td>
<td><input type='checkbox' name='21[]' value='Iron ore mining (212210)' />212210 Iron ore mining</td>
<td><input type='checkbox' name='21[]' value='Gold ore mining (212221)' />212221 Gold ore mining</td>
</tr>
<tr>
...
<tr>
<td><input type='checkbox' name='42[]' value='Motor vehicle and motor vehicle parts and supplies merchant wholesalers (4231)' />4231 Motor vehicle and motor vehicle parts and supplies merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Automobile and other motor vehicle merchant wholesalers (423110)' />423110 Automobile and other motor vehicle merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Motor vehicle supplies and new parts merchant wholesalers (423120)' />423120 Motor vehicle supplies and new parts merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Tire and tube merchant wholesalers (423130)' />423130 Tire and tube merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Motor vehicle parts (used) merchant wholesalers (423140)' />423140 Motor vehicle parts (used) merchant wholesalers</td>
</tr>
<tr>
<td><input type='checkbox' name='42[]' value='Furniture and home furnishing merchant wholesalers (4232)' />4232 Furniture and home furnishing merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Furniture merchant wholesalers (423210)' />423210 Furniture merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Home furnishing merchant wholesalers (423220)' />423220 Home furnishing merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Lumber and other construction materials merchant wholesalers (4233)' />4233 Lumber and other construction materials merchant wholesalers</td>
<td><input type='checkbox' name='42[]' value='Lumber, plywood, millwork, and wood panel merchant wholesalers (423310)' />423310 Lumber, plywood, millwork, and wood panel merchant wholesalers</td>
</tr>

 

which goes to "entry.php":

...
$mining=$_POST['21'];
...
$wholesale=$_POST['42'];
...

$query = "INSERT INTO report_notifications VALUES ('', '$email', '$agriculture', '$mining', 
'$utilities', '$construction', '$manufacturing', '$wholesale', '$retail', '$transportation', 
'$information', '$finance', '$professional', '$management', '$administration', '$education', '$healthcare', '$arts', 
'$accommodation', '$other')";

 

1.  Is this the best way to insert the values?  When I look at the database, I see arrays.  And, when I try to display my results in the "retrieve.php" page:

$query="SELECT * FROM report_notifications";
if ($result=mysql_query($sql)) {
    while ($row=mysql_fetch_row($result)) {
echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$email=mysql_result($result,$i,"email");
$mining=mysql_result($result,$i,"mining");
$utilities=mysql_result($result,$i,"utilities");
$construction=mysql_result($result,$i,"construction");
$manufacturing=mysql_result($result,$i,"manufacturing");
$wholesale=mysql_result($result,$i,"wholesale");
...
$serMi = serialize($mining);
$arrMi = unserialize(urldecode($serMi));
echo "<div class='forus'>";
var_dump($arrMi);
echo "</span><br /><br />";

$serUt = serialize($utilities);
$arrUt = unserialize(urldecode($serUt));
echo "<div class='forus'>";
var_dump($arrUt);
echo "</span><br /><br />";

$serCon = serialize($construction);
$arrCon = unserialize(urldecode($serCon));
echo "<div class='forus'>";
var_dump($arrCon);
echo "</span><br /><br />";


$serMa = serialize($manufacturing);
$arrMa = unserialize(urldecode($serMa));
echo "<div class='forus'>";
var_dump($arrMa);
echo "</span><br /><br />";


$serWh = serialize($wholesale);
$arrWh = unserialize(urldecode($serWh));
echo "<div class='forus'>";
var_dump($arrWh);
echo "</span><br /><br />";

 

All I get is

This is the contents of each Array:
Record #:1
NULL

string(5) "Array"

string(0) ""

string(0) ""

string(0) ""

string(0) ""

string(0) ""

string(0) ""

string(0) ""

string(0) "" 

 

What is the best way to insert and retrieve these values??

 

Thanks!

 

Joe

Link to comment
Share on other sites

And, by the way, when I put the following on my entry.php page

echo "<br />This is the contents of each Array:</span><br />";
...
$serMi = serialize($mining);
$arrMi = unserialize(urldecode($serMi));
echo "<div class='forus'>";
var_dump($arrMi);
echo "</span><br /><br />";
...
$serWh = serialize($wholesale);
$arrWh = unserialize(urldecode($serWh));
echo "<div class='forus'>";
var_dump($arrWh);
echo "</span><br /><br />";

 

 

I get basically what I want:

This is the contents of each Array:
NULL

array(4) { [0]=> string(51) "Crude petroleum and natural gas extraction (211111)" [1]=> string(38) "Natural gas liquid extraction (211112)" [2]=> string(18) "Coal mining (2121)" [3]=> string(51) "Bituminous coal and lignite surface mining (212111)" }

array(4) { [0]=> string(39) "Hydroelectric power generation (221111)" [1]=> string(36) "Electric power distribution (221122)" [2]=> string(31) "Natural gas distribution (2212)" [3]=> string(33) "Natural gas distribution (221210)" }

 

This is the same code I put on my retrieve.php page but on that page I get bumpkiss.  Why?

Link to comment
Share on other sites

Sasa,

 

Of course you do... how dopey of me!  ;D

 

Last (?) question:

 

My output looks much better:

Record #:1
e-mail: j@atm.gov
healthcare: array(6) { [0]=> string(28) "Offices of physicians (6211)" 
[1]=> string(65) "Offices of physicians (except mental health specialists) (621111)"
[2]=> string(57) "Offices of physicians, mental health specialists (621112)"
[3]=> string(44) "Offices of other health practitioners (6213)"
[4]=> string(33) "Offices of chiropractors (621310)"
[5]=> string(32) "Offices of optometrists (621320)" }
arts: array(2) { [0]=> string(24) "Dance companies (711120)" 
[1]=> string(35) "Musical groups and artists (711130)" }
other: array(4) { [0]=> string(29) "Personal care services (8121)" 
[1]=> string(37) "Other personal care services (812199)" 
[2]=> string(39) "Drycleaning and laundry services (8123)" 
[3]=> string(48) "Coin-operated laundries and drycleaners (812310)" } 

 

but I want to pretty it up.

 

What is a good way (with just a snipit of code if you please) to grab the number of items in the array, run a while loop (or similar), strip out the stuff like array(3) { [0]=> string(40) and put it in a list?

 

Thanks!!

Link to comment
Share on other sites

Never mind... found the answer (Google is a wonderful thing!)

 

$ihc = count($healthcare);

if ($ihc == 0) {
echo 'Array is empty!';
}

echo 'Healthcare: ';
for ($i=0;$i<$ihc;$i++) {
echo $healthcare[$i] . ' ';
}	

gives

Healthcare: Offices of physicians (6211) Offices of physicians (except mental health specialists) (621111) Offices of physicians, mental health specialists (621112) Offices of other health practitioners (6213) Offices of chiropractors (621310) Offices of optometrists (621320) 

 

Just need to pretty up a little more but that part is easy.

 

Thanks for your help, sasa!!

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.