Jump to content

[SOLVED] Display Array's properly


timmah1

Recommended Posts

I have a preference page that pulls info from the database

$itemCodes;
//NCAA Basketball
	$itemCodes["bball_ncaa_sngl"] = "$a[bball_ncaa_sngl]";
	$itemCodes["bball_ncaa_lck"] =  "$a[bball_ncaa_lck]";
	$itemCodes["bball_ncaa_mnth"] = "$a[bball_ncaa_mnth]";
	$itemCodes["bball_ncaa_ssn"] = "$a[bball_ncaa_ssn]";
	$itemCodes["bball_ncaa_po"] = "$a[bball_ncaa_po]";
	$itemCodes["bball_ncaa_spl"] = "$a[bball_ncaa_spl]";
	$NCAABactive["bball_ncaa_active"] = "$a[bball_ncaa_active]";

$itemCodes;		
//NBA
	$itemCodes["bball_sngl"] = "$a[bball_sngl]";
	$itemCodes["bball_lck"] =  "$a[bball_lck]";
	$itemCodes["bball_mnth"] = "$a[bball_mnth]";
	$itemCodes["bball_ssn"] = "$a[bball_ssn]";
	$itemCodes["bball_po"] = "$a[bball_po]";
	$itemCodes["bball_spl"] = "$a[bball_spl]";
	$NBAactive["bball_active"] = "$a[bball_active]";

 

I have a page that refers to the preference page to show the variables

 

if($NBAactive['bball_active'] == "1") { ?>
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>     
<!--start NBA table-->
<td class="orderHeader" align="center">NBA Basketball</td>
</tr>
<tr>
<td>
<?php 
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "nba") == 0){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='nba' value='$fieldName'> $descr: $price<br>\n";
   }
}
echo $buffer;
unset ($buffer);
reset($itemCodes);
?>
</td>
</tr>
</table>
<!--end NBA table-->
<?php } ?>
</td>
<td width="0"> </td>
<td width="0"></td>
<td width="0"> </td>
<td width="400">

<?php
if($NCAABactive['bball_ncaa_active'] == "1") { ?>
<!--start NCAA Basketball table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>
<td class="orderHeader" align="center">College Basketball Specials</td>
</tr>
<tr>
<td>
<? 
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "ncaab") == 0){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type=radio name=ncaab value=$fieldName> $descr: $price<br>\n";
   }
}
echo $buffer;
unset ($buffer);
reset($itemCodes);
?></td>
</tr>
</table> 

 

It pulls everything out correctly, but it's not formatting the information correctly.

 

I have once box for NBA, one for NCAA Basketball, and the array's should go where the code is accordingly.

The problem is, every variable is showing up in both boxes.

For example, under the NBA box, it shows the NBA and the NCAAB, and the same thing for the NCAA Basketball box.

 

How do I get it so that the correct items are in the correct boxes?

 

Thanks in advance

 

 

Link to comment
Share on other sites

<?php
if($NBAactive['bball_active'] == "1") { ?>
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>     
<!--start NBA table-->
<td class="orderHeader" align="center">NBA Basketball</td>
</tr>
<tr>
<td>
<?php 
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "ncaa") === false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='nba' value='$fieldName'> $descr: $price<br>\n";
   }
}
echo $buffer;
unset ($buffer);
reset($itemCodes);
?>
</td>
</tr>
</table>
<!--end NBA table-->
<?php } ?>
</td>
<td width="0"> </td>
<td width="0"></td>
<td width="0"> </td>
<td width="400">

<?php
if($NCAABactive['bball_ncaa_active'] == "1") { ?>
<!--start NCAA Basketball table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>
<td class="orderHeader" align="center">College Basketball Specials</td>
</tr>
<tr>
<td>
<?php
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "ncaa") !== false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type=radio name=ncaab value=$fieldName> $descr: $price<br>\n";
   }
}
?>

Link to comment
Share on other sites

This produces the exact same thing that I was getting.

 

Here is the entire code

<?php
if($NBAactive['bball_active'] == "1") { ?>
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>     
<!--start NBA table-->
<td class="orderHeader" align="center">NBA Basketball</td>
</tr>
<tr>
<td>
<?php 
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "nba") === false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='nba' value='$fieldName'> $descr: $price<br>\n";
   }
}
echo $buffer;
unset ($buffer);
reset($itemCodes);

?>
</td>
</tr>
</table>
<!--end NBA table-->
<?php } ?>
</td>
<td width="0"> </td>
<td width="0"></td>
<td width="0"> </td>
<td width="400">

<?php
if($NCAABactive['bball_ncaa_active'] == "1") { ?>
<!--start NCAA Basketball table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>
<td class="orderHeader" align="center">College Basketball Specials</td>
</tr>
<tr>
<td>
<? 
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "ncaab") === false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='ncaab' value='$fieldName'> $descr: $price<br>\n";
   }
}
echo $buffer;
unset ($buffer);
reset($itemCodes);
?></td>
</tr>
</table> 
<!--end NCAA Basketball table-->
<?php } ?>
</td>
</tr>
<tr valign="top">
<td width="400">

<?php
if($NCAAFactive['fball_ncaa_active'] == "1") { ?>
<!--start NCAA Football table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>       
<td class="orderHeader" align="center">College Football Specials</td>
</tr>
<tr>
<td>
<?php 
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "ncaaf") === false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='ncaaf' value='$fieldName'> $descr: $price<br>\n";
   }
}
echo $buffer;
unset ($buffer);
reset($itemCodes);

?></td>
</tr>
</table>
<!--end NCAA Football table-->
<?php } ?>
</td>
<td width="0"> </td>
<td width="0">

<!--start Blank table-->
<table width="0%" cellpadding="0" border="0" cellspacing="0">
<tr>

<td class="orderHeader" align="center"></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<!--end NCAA FB table-->

</td>
<td width="0"> </td>
<td width="400">

<?php
if($NFLactive['fball_active'] == "1") { ?>
<!--start NFL table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>       
<td class="orderHeader" align="center">NFL Pro Football Specials</td>
</tr>
<tr>
<td>
<?php
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "nfl") === false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='nfl' value='$fieldName'> $descr: $price<br>\n";
   }
}
echo $buffer;
unset ($buffer);
reset($itemCodes);
?></td>
</tr>
</table>
<!--end NFL table-->
<?php } ?>
</td>
</tr>
<tr valign="top">
  <td colspan="5">
<?php
if($MLBactive['upick_active'] == "1") { ?>
<!--start Ultimate table-->
  <table width="400" border="1" align="center" cellpadding="3" cellspacing="0">
    <tr>      
      <td class="orderHeader" align="center">Ultimate Lock Pick</td>
    </tr>
    <tr>
      <td>
  <?php
foreach ($itemCodes as $fieldName => $val){
   if (strpos($fieldName, "ult") !== false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='ult' value='$fieldName'> $descr: $price<br>\n";
   }
}

?>
</td>
    </tr>
  </table>
<!--End MLB table-->
<?php } ?> 

 

Here is the preferences code

$itemCodes;
//MLB
	$itemCodes["upick_sngl"] = "$a[upick_sngl]";
	$itemCodes["upick_lck"] =  "$a[upick_lck]";
	$itemCodes["upick_mnth"] = "$a[upick_mnth]";
	$itemCodes["upick_ssn"] = "$a[upick_ssn]";
	$itemCodes["upick_po"] = "$a[upick_po]";
	$itemCodes["upick_spl"] = "$a[upick_spl]";
	$MLBactive["upick_active"] = "$a[upick_active]";

//NFL
	$itemCodes["fball_sngl"] = "$a[fball_sngl]";
	$itemCodes["fball_lck"] =  "$a[fball_lck]";
	$itemCodes["fball_mnth"] = "$a[fball_mnth]";
	$itemCodes["fball_ssn"] = "$a[fball_ssn]";
	$itemCodes["fball_po"] = "$a[fball_po]";
	$itemCodes["fball_spl"] = "$a[fball_spl]";
	$NFLactive["fball_active"] = "$a[fball_active]";

//NCAA Football
	$itemCodes["fball_ncaa_sngl"] = "$a[fball_ncaa_sngl]";
	$itemCodes["fball_ncaa_lck"] =  "$a[fball_ncaa_lck]";
	$itemCodes["fball_ncaa_mnth"] = "$a[fball_ncaa_mnth]";
	$itemCodes["fball_ncaa_ssn"] = "$a[fball_ncaa_ssn]";
	$itemCodes["fball_ncaa_po"] = "$a[fball_ncaa_po]";
	$itemCodes["fball_ncaa_spl"] = "$a[fball_ncaa_spl]";
	$NCAAFactive["fball_ncaa_active"] = "$a[fball_ncaa_active]";

//NCAA Basketball
	$itemCodes["bball_ncaa_sngl"] = "$a[bball_ncaa_sngl]";
	$itemCodes["bball_ncaa_lck"] =  "$a[bball_ncaa_lck]";
	$itemCodes["bball_ncaa_mnth"] = "$a[bball_ncaa_mnth]";
	$itemCodes["bball_ncaa_ssn"] = "$a[bball_ncaa_ssn]";
	$itemCodes["bball_ncaa_po"] = "$a[bball_ncaa_po]";
	$itemCodes["bball_ncaa_spl"] = "$a[bball_ncaa_spl]";
	$NCAAFactive["bball_ncaa_active"] = "$a[bball_ncaa_active]";

//NBA
	$itemCodes["bball_sngl"] = "$a[bball_sngl]";
	$itemCodes["bball_lck"] =  "$a[bball_lck]";
	$itemCodes["bball_mnth"] = "$a[bball_mnth]";
	$itemCodes["bball_ssn"] = "$a[bball_ssn]";
	$itemCodes["bball_po"] = "$a[bball_po]";
	$itemCodes["bball_spl"] = "$a[bball_spl]";
	$MLBactive["bball_active"] = "$a[bball_active]";

Link to comment
Share on other sites

Change you array to a 2-d array:

<?php
$itemCodes = array('mlb'=>array(),'nfl'=>array());
//MLB
	$itemCodes['mlb]["upick_sngl"] = $a['upick_sngl'];
	$itemCodes['mlb]["upick_lck"] =  $a['upick_lck'];
	$itemCodes['mlb]["upick_mnth"] = $a['upick_mnth'];
	$itemCodes['mlb]["upick_ssn"] = $a['upick_ssn'];
	$itemCodes['mlb]["upick_po"] = $a['upick_po'];
	$itemCodes['mlb]["upick_spl"] = $a['upick_spl'];
	$MLBactive = $a['upick_active'];

//NFL
	$itemCodes['nfl']["fball_sngl"] = $a['fball_sngl'];
	$itemCodes['nfl']["fball_lck"] =  $a['fball_lck'];
	$itemCodes['nfl']["fball_mnth"] = $a['fball_mnth'];
	$itemCodes['nfl']["fball_ssn"] = $a['fball_ssn'];
	$itemCodes['nfl']["fball_po"] = $a['fball_po'];
	$itemCodes['nfl']["fball_spl"] = $a['fball_spl'];
	$NFLactive = $a['fball_active]";
?>

Then to display the values:

<?php
<?php
if($NBAactive) { ?>
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>     
<!--start NBA table-->
<td class="orderHeader" align="center">NBA Basketball</td>
</tr>
<tr>
<td>
<?php
$buffer = '';
foreach ($itemCodes['nba'] as $fieldName => $val){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='nba' value='$fieldName'> $descr: $price<br>\n";
}
echo $buffer;

?>
</td>
</tr>
</table>
<!--end NBA table-->
<?php } ?>
</td>
<td width="0"> </td>
<td width="0"></td>
<td width="0"> </td>
<td width="400">

<?php
if($NCAABactive) { ?>
<!--start NCAA Basketball table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>
<td class="orderHeader" align="center">College Basketball Specials</td>
</tr>
<tr>
<td>
<? 
$buffer = '';
foreach ($itemCodes['ncaab'] as $fieldName => $val){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='ncaab' value='$fieldName'> $descr: $price<br>\n";
}
echo $buffer;
?></td>
</tr>
</table> 
<!--end NCAA Basketball table-->
<?php } ?>
</td>
</tr>
<tr valign="top">
<td width="400">

<?php
if($NCAAFactive) { ?>
<!--start NCAA Football table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>       
<td class="orderHeader" align="center">College Football Specials</td>
</tr>
<tr>
<td>
<?php
$buffer = ''; 
foreach ($itemCodes['ncaaf'] as $fieldName => $val){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='ncaaf' value='$fieldName'> $descr: $price<br>\n";
}
echo $buffer;
?></td>
</tr>
</table>
<!--end NCAA Football table-->
<?php } ?>
</td>
<td width="0"> </td>
<td width="0">

<!--start Blank table-->
<table width="0%" cellpadding="0" border="0" cellspacing="0">
<tr>

<td class="orderHeader" align="center"></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<!--end NCAA FB table-->

</td>
<td width="0"> </td>
<td width="400">

<?php
if($NFLactive) { ?>
<!--start NFL table-->
<table width="100%" cellpadding="3" border="1" cellspacing="0">
<tr>       
<td class="orderHeader" align="center">NFL Pro Football Specials</td>
</tr>
<tr>
<td>
<?php
$bufer = '';
foreach ($itemCodes['nfl'] as $fieldName => $val){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='nfl' value='$fieldName'> $descr: $price<br>\n";
}
echo $buffer;
?></td>
</tr>
</table>
<!--end NFL table-->
<?php } ?>
</td>
</tr>
<tr valign="top">
  <td colspan="5">
<?php
$buffer = '';
if($MLBactive) { ?>
<!--start Ultimate table-->
  <table width="400" border="1" align="center" cellpadding="3" cellspacing="0">
    <tr>      
      <td class="orderHeader" align="center">Ultimate Lock Pick</td>
    </tr>
    <tr>
      <td>
  <?php
foreach ($itemCodes['mlb'] as $fieldName => $val){
   if (strpos($fieldName, "ult") !== false){
     list($descr, $price) = explode("|", $val);
     $price = ($price == "N/A")? "N/A": sprintf("%.2f", $price);
     $buffer .= "<input type='radio' name='ult' value='$fieldName'> $descr: $price<br>\n";
   }
}

?>
</td>
    </tr>
  </table>
<!--End MLB table-->
<?php } ?> 

 

Ken

 

 

Link to comment
Share on other sites

Thanks Ken

That works like it should.

 

My problem now is, on the next page, I have this for the order page

foreach ($_POST as $item => $itemCodesKey){
   if ($item == "nba" ||
       $item == "utl" ||
       $item == "nfl" ||
       $item == "ncaaf" ||
       $item == "ncaab" ) {
      list($desc, $price) = explode("|", $itemCodes[$itemCodesKey]);
      if ($price > 0){
         $hidden .= "<input type='hidden' name='$itemCodesKey' value='on'>\n";
      } else {
         continue;
      }
   }
}

 

And then this as the review page

 

foreach ($_POST as $item => $val){
   if ($val == "on") {
      list($descript, $price) = explode("|", $itemCodes[$item]);
      $price *= $factor;
      $buffer .= "<td align='center'>$descript </td>";
      $buffer .= "<td align='center'>'".sprintf("%.2f</td>\n", $price);
      $buffer .= "</tr><tr>\n";
      $ttlCharge += $price;
      $hidden .= "<input type='hidden' name='$item' value='";
      $hidden .= sprintf("%.2f' >\n", $price);
   }
}

 

And using the 2-d array is not showing anything that was ordered

 

How would I change this now to reflect using a 2-d array?

Link to comment
Share on other sites

I'm not 100% what you are trying to do here, but here is your updated code that should work with the 2d array.

 

<?php
foreach ($_POST as $item => $itemCodesKey){
   if ($item == "nba" ||
       $item == "utl" ||
       $item == "nfl" ||
       $item == "ncaaf" ||
       $item == "ncaab" ) {
      list($desc, $price) = explode("|", $itemCodes[$item][$itemCodesKey]);
      if ($price > 0){
         $hidden .= "<input type='hidden' name='$item' value='$itemCodesKey'>\n";
      } else {
         continue;
      }
   }
}
?>

 

<?php
foreach ($_POST as $item => $itemCodesKey){
      list($descript, $price) = explode("|", $itemCodes[$item][$itemCodesKey]);
      $price *= $factor;
      $buffer .= "<td align='center'>$descript </td>";
      $buffer .= "<td align='center'>'".sprintf("%.2f</td>\n", $price);
      $buffer .= "</tr><tr>\n";
      $ttlCharge += $price;
      $hidden .= "<input type='hidden' name='$item' value='";
      $hidden .= sprintf("%.2f' >\n", $price);
}
?>

Link to comment
Share on other sites

Thank you so much.

the only problem that it has now, on the bottom code, it display's the correct item and price, with lot's of other 0.00's added.

 

You can see what it is here http://cheezyfries.net/vegasD/ and how it ends up showing the results.

 

Don't fill anything out, it's not necessary at this point, just click continue and you'll see what I mean.

Link to comment
Share on other sites

Yeah, looks like you need to add that check into the last bit of code:

 

<?php
<?php
foreach ($_POST as $item => $itemCodesKey){
    if ($item == "nba" ||
       $item == "utl" ||
       $item == "nfl" ||
       $item == "ncaaf" ||
       $item == "ncaab" ) {
      list($descript, $price) = explode("|", $itemCodes[$item][$itemCodesKey]);
      $price *= $factor;
      $buffer .= "<td align='center'>$descript </td>";
      $buffer .= "<td align='center'>'".sprintf("%.2f</td>\n", $price);
      $buffer .= "</tr><tr>\n";
      $ttlCharge += $price;
      $hidden .= "<input type='hidden' name='$item' value='";
      $hidden .= sprintf("%.2f' >\n", $price);
   }
}
?>
?>

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.