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
Share on other sites

Yeah, You're not really clear with your examples of needs of the script. If you tell us what you're trying to do (if we didn't answer your question) then we may be able to find a more useful way of coding for that.

Link to comment
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"] . ")";

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

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.