Jump to content

Array not producing what I intended


webguync

Recommended Posts

Anyone know what I am doing wrong here?

<?php
<?php
$Question["1"] = '1-A';
$Question["2"] = '2-A';
$Question["3"] = '1-A';
$Question["4"] = '2-A';
$Question["5"] = '1-B';



echo "1 - $" . $Question["1"] . "<br />";
echo "2 - $" . $Question["2"] . "<br />";
echo "3 - $" . $Question["3"] . "<br />";
echo "4 - $" . $Question["4"] . "<br />";
echo "5 - $" . $Question["5"] . "<br />";

?>

 

ouput

 

1 - $1-A

2 - $2-A

3 - $1-A

4 - $2-A

5 - $1-B

 

want it to be

 

1(I-A), 2(I-A),3(I-A) etc.

 

 

Link to comment
https://forums.phpfreaks.com/topic/195844-array-not-producing-what-i-intended/
Share on other sites

<?php
$Question["1"] = '1-A';
$Question["2"] = '2-A';
$Question["3"] = '1-A';
$Question["4"] = '2-A';
$Question["5"] = '1-B';



echo "1 - (" . $Question["1"] . ")<br />";
echo "2 - (" . $Question["2"] . ")<br />";
echo "3 - (" . $Question["3"] . ")<br />";
echo "4 - (" . $Question["4"] . ")<br />";
echo "5 - (" . $Question["5"] . ")<br />";

?>

or

<?php
$Question["1"] = '1-A';
$Question["2"] = '2-A';
$Question["3"] = '1-A';
$Question["4"] = '2-A';
$Question["5"] = '1-B';



echo "1(" . $Question["1"] . "),";
echo "2(" . $Question["2"] . "),";
echo "3(" . $Question["3"] . "),";
echo "4(" . $Question["4"] . "),";
echo "5(" . $Question["5"] . ")";

?>

ultimately what I need  is to set up a column in html which corresponds with values from another column being pulled from MySQL db.

 

so it would look like <td class="colA">1,4,6,8,9,11,15</td><td class="colB>1-A,1-A,1-B,1-B,1-C,1-D,II-B</td>

 

keep in mind that the numbers in col A are variable and are being pulled from MySQL records.

 

I thought an associative array would get me where I wanted to go, but I could be wrong...

BTW, what Deivas posted is what I am looking for as far as association. I just need to figure out how to get that done dynamically.

</td>1,4,6,8,9,11,15<td class="colB>1-A,1-A,1-B,1-B,1-C,1-D,II-B

 

$a = Array(1,4,6,8,9,11,15);
$b = Array('1-A','1-A','1-B','1-B','1-C','1-D','II-B');

$combined = array_merge($a, $b);

//1-A => 1, 1-A => 4 ...
$questions = array_flip($combined);

I have a few questions on this method. Since the results from $a  would be coming from a database field and the results are variable how would I handle this?

 

Maybe it would help if I posted the code I have to display.

 

<table id="resultlist">
   
	<tr>
		<th scope="col">Employee Name</th>
                        
		<th scope="col">Number Correct</th>
		<th scope="col">Score</th>
		<th scope="col">Question Number Answered Incorrectly</th>
		<th scope="col">Date Completed</th>
                       
                       <th scope="col">Pass/Fail</th>
		<th scope="col">Material to review (Chapter, Section)</th>
	</tr>

	<?php if (!isset($name)) { ?>
	<tr><td colspan="7">There are no scores to display</td></tr>
	<?php
	} else {
	for ($i=0; $i<count($name); $i++) { ?>
	<tr class="<?php echo $i%2 ? 'hilite' : 'nohilite'; ?>">
		<td ><?php echo $name[$i];?></td>
		<td><?php echo $numCorr[$i];?></td>
            

		<td><?php echo (ROUND(($pcnt[$i]*100),0).'%'); ?></td>
		<td><?php echo $incorr[$i];?></td>
		<td><?php echo (date('F j, Y  g:i A',($date[$i])));?></td>
          <td><?php
   if(($pcnt[$i]*100) > 89)
   {
      echo "<div class='passed'>" .Passed."</div>";
   }
   else
   {
      echo "<div class='failed'>" .Failed. "</div>";
   }
?></td>


<td><?php echo $workon[$i];?></td>
</tr>
	<?php }
	} ?>

</table>



 

the field $incorr is the one with the variable responses, and the field $workon is where I want to corresponding array values to appear. Currently I am entering manually in the database, but would prefer to have the association automated. I could also work with in the $incorr[$i] field displaying 1(I-A),2(I-B) or something like that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.