Jump to content

How do I stop duplicate array entries?


FourEyes

Recommended Posts

I am reading data out of a MySQL table. I know there are 144 fields per record, and I've checked the data to ensure this is the case. However, when I read the values into an array, I end up with 288 elements in my array. Array element 0 is duplicated in element 1, 2 is duplicated in 3, etc. What's causing this and how do I stop it?

<?php
session_start();
header("Cache-control: private");
$person_id = $_SESSION['page'];
$customer_id = $_SESSION['stamp'];
$c = $_SESSION['c'];
$m1 = $_SESSION['m1'];
$m2 = $_SESSION['m2'];
$medical = array(array());
include 'header.html';

# Connect to MySQL
$connection = @mysql_connect("localhost", "???", "???") or die("Could not connect to MySQL.");

# Select the specified database.
$result = @mysql_select_db("sfb", $connection) or die("Could not select the requested database.");
?>

<table width=175 border=0 align="left" cellpadding=0 cellspacing=0>
<tr align="left" valign="top" nowrap>
<td width=175 valign="top" nowrap><img src="../images/NAV-pic.jpg" border=0></td>
</tr><tr align="left" valign="top" nowrap>
<td align="left" nowrap><a href="index.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','NAV-home-on.gif',0)"><img src="../images/NAV-home.gif" alt="home" name="Image6" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
<td align="left" nowrap><a href="products.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image7','','NAV-products-on.gif',0)"><img src="../images/NAV-products.gif" alt="Products & Services" name="Image7" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
<td align="left" nowrap><a href="csa.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image19','','NAV-service-on.gif',0)"><img src="../images/NAV-service-on.gif" alt="Customer Service Area" name="Image19" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
<td align="left" nowrap><a href="quotes.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image9','','NAV-quotes-on.gif',0)"><img src="../images/NAV-quotes.gif" alt="Online Quotes" name="Image9" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
<td align="left" nowrap><a href="free.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image17','','NAV-info-on.gif',0)"><img src="../images/NAV-info.gif" alt="Free Information" name="Image17" border=0></a></td>
</tr><tr align="left" valign="top" nowrap>
<td align="left" nowrap><a href="about.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image18','','NAV-about-on.gif',0)"><img src="../images/NAV-about.gif" alt="About Us" name="Image18" width=175 height=24 border=0></a></td>
</tr>
</table>

<?php
$self = $_SERVER['PHP_SELF'];
echo ("<form action=\"$self\" method=\"post\">");
?>

<h2><?php echo $c;?><br>Add/Edit/Delete Major Medical Plan</h2>
<select name="Plans" size=5>
<?php
$query = "select * from medical order by plan_name";
$result = mysql_query($query,$connection) or die ("Could not find any plans.");

$i = 0;
while ($row = mysql_fetch_array($result)) {
$id = $row['plan_id'];
if ($id == $_POST['Plans']) {
echo (" <option selected value=");
} else {
echo (" <option value=");
}
echo $id . ">" . $row['plan_name'] . " ~ " . $row['carrier'];
if ($row['network'] == "Y") {
echo " ~ Network</option>\n";
} else {
echo " ~ Non-network</option>\n";
}

$j = 0;
foreach ($row as $info) {
$medical[$i][$j] = $info;
$j++;
}

$i++;
}
?>
</select><br>
Select a plan, then click, <input type="submit" name="View" value="View Plan Details"><br><br>
<table width=595 border=0 cellpadding=0 cellspacing=0>
<?php
echo $medical[0][2],$medical[1][2];
echo $medical[0][3],$medical[1][3];
# The two lines above produce the same results = Badness.
for ($k=4;$k<$j;$k=$k+7) {
echo "<tr><td>" . $medical[0][$k] . "</td></tr>";
}
?>
</table>
<!--#include virtual="footer.html" -->
Link to comment
Share on other sites

[!--quoteo(post=383002:date=Jun 12 2006, 06:41 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Jun 12 2006, 06:41 PM) [snapback]383002[/snapback][/div][div class=\'quotemain\'][!--quotec--]
change this:

while ($row = mysql_fetch_array($result)) {
to this:

while ($row = mysql_fetch_assoc($result)) {
Read up in the manual why!
[/quote]


<sound of hand slapping forehead>
From the PHP manual page on mysql_fetch_array:

Parameters

result
The result resource that is being evaluated. This result comes from a call to mysql_query().

result_type
The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.


Road hypnosis. I use mysql_fetch_array all the time, and just got so used to it I didn't *think* about using another fetch option. Thank you, ToPlay
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.