Status of questions in an online exam with php - php

I am still a novice in developing web App.
i have successfully designed an online exam web application in php. But i keep wondering, how can i make group of boxes that will help indicate to the user the questions he answered an those the user didnt. The boxes can also be uses to navigate through the series of questions on the web app
A web app with eah question status
A simple example of my explanation is the circle part of the image above.
I am so much intrested in how possible can i make the part of the interface possible and then how to make it work as a status of all questions.
I use the follwing code for the status of the questions
<?
$sql= "SELECT * FROM questions order by rand() LIMIT 20 ";
$_SESSION['questions'] = array();
$i=0;
foreach ($db_con->query($sql) as $rows) {
++$i;
$_SESSION['questions'][$i]=0;
echo "<span id='s". $i ."'>". $i ."</span> ";
}?>
<tr>
<td coLspan=”2”>
<h3>Question <?php $qnum=$rows['quesNo']; echo $i ?>: <label class="rchk" for="<?php echo $i ?>"> <?php echo $rows['ques']; ?> </label></h3> </td>
</tr>
<?php
$sqla= "SELECT * FROM answers where quesNo='$qnum'order by rand()";
foreach ($db_con->query($sqla) as $rowsa) {
?>
<tr>
<td>
<input type="radio" name="<?php echo $i ?>" id="<?php echo $i ?>" class="rchk" onclick='anstatus()' value="<?php echo $rowsa['ans_id']; ?>"/> <?php echo $rowsa['ans']; ?>
</td>
</tr>
The code above will generate spans for the numbers of questions available and for each question answered i want the tag for that question number to change the color to red. So i try the following Jquery code code
function anstatus(){
// #qcount is an hidden tag to keep the numbers of questions
var counter = $('#qcount').val();
j=1;
while (j<=counter){
if( $("input[name='"+j+"']:checked")){
// $(this).css("color", "red");
$('#s'+j+'').css("color", "red");
}else {
$('#s'+j+'').css("color", "blue");
}
j++;
}
}
After trying while loop. I tried a for loop. but the problem am facing is that all the tags changes color when a question is answered instead of only the answered question.
help me out please

My first thought is to use a $_SESSION array. When the user loads the quiz, set:
$_SESSION['questions'] = array(0,0,0,0,0);
Then when a user answers a question, set the corresponding element in the array to 1 (remembering that the first question is number 0, the 2nd question is number 1 etc).
eg, if user answers question 3:
$_SESSION['questions'][2] = 1;
That way you can use loops and array functions to do whatever you want to do with the data.

I use the following method to solve
Firstly, I created the span tag with the following code
<?php
$sql= "SELECT * FROM questions order by rand() LIMIT 20 ";
$_SESSION['questions'] = array();
$i=0;
foreach ($db_con->query($sql) as $rows) {
++$i;
$_SESSION['questions'][$i]=0;
echo "<span class='border' id='s". $i ."' syle='color:white'>". $i ."</span> ";
}
I made the following code for my question list
<tr>
<td coLspan=”2”>
<h3>
Question <?php $qnum=$rows['quesNo']; echo $i ?>:
<label class="rchk" for="<?php echo $i ?>">
<?php echo $rows['ques']; ?>
</label>
</h3></td>
</tr>
<?php
$sqla= "SELECT * FROM answers where quesNo='$qnum'order by rand()";
foreach ($db_con->query($sqla) as $rowsa) {
?>
<tr>
<td>
<input type="radio" name="<?php echo $i ?>" d="<?php echo $i ?>" id="<?php echo $i ?>" class="rchk" value="<?php echo $rowsa['ans_id']; ?>"/> <?php echo $rowsa['ans']; ?>
</td>
</tr>
Thirdly, I then added an external script and add it to the document using the html tag.
the content of the script is as follows
$(function() {
$(".rchk").click(function(){
var id = $(this).attr("d");
if( $("input[name='"+id+"']:checked")){
$('#s'+id+'').css("color", "red");
};
});
});

Related

Formatting dynamically generated checkboxes?

I've got a system that gathers information out of XML files, for the exporter I have been trying to create a system where it gathers the unique imprints from a SQL table then outputs them & uses it for the export query so we can export only what we are after at that time & not everything.
The code I have does the above fine however I've been trying to format it to sit within a table and every 7/8 iterations it starts a new table row however I've been unsuccessful at this point, with 167 rows currently I want it to look a bit better than a massive list (and easier to use).
The code I have currently is the following:
<table>
<form action="exporttrade.php" method="post">
<b>Please Select imprints you wish to Export for <client></b><br />
<?PHP
mysql_connect('server', 'user', 'password');
mysql_select_db("database");
$sql ="SELECT distinct imprint FROM table";
$results= mysql_query($sql);
while( $imprint = mysql_fetch_assoc($results) ):
?>
<tr><td><?php echo $imprint['imprint']; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint['imprint']; ?>"/></td></tr>
<?php endwhile; ?>
I've been attempting to use a counter and modulo function I've seen online to insert code every nth line (in this case will be the ending < /tr> tag. Does anyone here know how you can sucessfully do what I am after? I'm just a bit stumped at how to format this currently :/
Edit - Just a heads up managed to get this working as I wanted it to now used the following:
$imprints=grab_array($sql);
foreach ($imprints as $imprint){
$imprint=$imprint['imprint'];
$counter++;
if($counter % 8 == 0){
echo "</tr><tr>";
}
?>
<td><?php echo $imprint; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint; ?>"/></td>
<?PHP
}
?>
Try this code.
<table>
<form action="exporttrade.php" method="post">
<b>Please Select imprints you wish to Export for <client></b><br />
<?PHP
mysql_connect('server', 'user', 'password');
mysql_select_db("database");
$count = 0;
$sql ="SELECT distinct imprint FROM table";
$results= mysql_query($sql);
while( $imprint = mysql_fetch_assoc($results) ):
if($imprint)
{
count++;
}
while($count <= 8)
?>
<tr><td><?php echo $imprint['imprint']; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint['imprint']; ?>"/></td></tr>
<?php
$count = 0; echo '<tr><td> New Row after 8 </td> </tr>'?>
<?php continue ?>
<?php endwhile; ?>
<?php endwhile; ?>
you can use this logic.

Displaying content but not the latest one

I have these codes to display content on my website.
Index.php
$rest = new rest;
$list = $rest->fetch_all();
<?php foreach ($rest->fetch_all() as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
And include.php
class rest {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM podcast ORDER BY cast_id DESC");
$query->execute();
return $query->fetchAll();
} }
Please can someone tell me how I can get this to show results but not the latest result?
All fields are numbered using an ID tag in mysql.
On my site I have the latest entry listed above this list so I do not require the latest one appearing here.
Please help.
Thank you.
The easiest way to do this is to discard the row in the application. You can do it in SQL by using a calculated limit. Calculate it as (SELECT COUNT(*) FROM podcast) - 1.
Or:
SELECT *
FROM podcast
EXCEPT
SELECT TOP 1 *
FROM podcast
ORDER BY cast_id ASC
Excluding the last row.
You can either use array_shift or array_pop depending on your query sorting as shown bellow:
Assuming the latest result is the first raw on your query result.
$rest = new rest;
$list = $rest->fetch_all();
$latest = array_shift($list);
<?php foreach ($list as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
If it's the last raw that you need to hide, then use array_pop

javascript based slideshow not working

I'm trying to implement a simple javascript/jquery-1.6.2 slideshow (I'm not used to javascript), the problem I have is that the items are all shown and I don't know how to hide them.
here is my PHP/HTML code:
<?php
$c = 0;
// show parts of latest news:
foreach ($items AS $data) {
// news image
$image = './images/'.$data['image'];
// description
$part_description = substr(nl2br($data['content']), 0, 120);
?>
<div id="cap_<?php echo $c; ?>" class="cap" >
<h3><a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
<?php echo $data['title']; ?></a></h3>
<table>
<tr>
<td>
<a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
<img alt="Item image" width="200"
src="<?php echo $image; ?>" />
</a>
</td>
<td>
<strong>By</strong>
<?php echo $data['author']; ?>
<br/>
</td>
<tr/>
</table>
<p>
<?php echo stripslashes($part_description); ?>
<a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
read more
</a>
</p>
</div>
<?php
$c++;
}
// show links:
?>
<div class="links">
<?php
for ($i=0; $i<$k; $i++) {
?>
<a href="javascript:slideShow(<?php echo $i; ?>, <?php echo $k; ?>);" class="link_number" id="link_<?php echo $i; ?>">
<?php echo $i+1; ?>
</a>
<?php
}
?>
And my js function:
function slideShow(id, max) {
var inputHide = null;
for (var i=0; i<max; i++) {
$('#cap_'+id).fadeOut(500);
inputHide = document.getElementById('cap_'+i);
document.getElementById('link_'+i).style.backgroundColor = '#999999';
}
$('#cap_'+id).fadeIn(500);
document.getElementById('link_'+id).style.backgroundColor = 'black';
}
I have two questions:
Q1: how can I fix this problem and show only one
Q2: is it better if I use a ready-to-use script like jqueryui?
In terms of your second question, It depends on what you need, and your understanding of JS. If you have a pure JS script, and you understand how and why it works, you can tailor it to your needs more. 'personalise it', if you like. However if your knowledge of JS is limited, then a ready to use script may suit you better.
A good way to learn things is to find a pure JS script somewhere (probably many on here), and use that one, but do your research and use your initiative until you understand how it works. Then you can use it in other projects, change it, use bits of it. That's how I learnt most of my javascript!
I've had some trouble with slideshows recently too, good luck! :)
Q1: this should work: inputHide.style.display = 'none';
Q2: In my opinion, both ways are good

Dynamic data population in table form with Code Igniter

I have a pretty simple question, but for some reason I am drawing a blank. I have the following code in my view file, and I want to display the results in a two column table, so the first entry would be on the left, the next would be on the right then the next one after that would be below the first row, and eventually I will use the pagination class (haven’t gotten that far yet) For some reason I can not figure out how to get the results to display in a 2 column format… only one. Any help would be greatly appreciated.
Ideally I would like to have 4 columns, but the code below was started with just the idea of 2 columns.
Thanks!
<table>
db->query($sql);
foreach ($query->result() as $row)
{
echo("");
echo("");
echo $row->Title;
echo ("<br/>");
?>
<img name="<?php echo $row->Thumb;?>" src="../uploaded/portfolio/thumbs/<?php echo $row->Thumb;?>" alt="">
<?php
echo("<br/>");
echo $row->DescText;
echo("</td>");
echo("<td>");
// Display next picture here
echo("</td>");
echo("</tr>");
}
?>
../
Your code example is rather confusing, but I think from your description that you're trying to do something like this:
<table>
<tr>
<?php $i = 0; foreach($query->result() as $row): ?>
<?php if ($i % 2 == 0): ?>
</tr><tr>
<?php endif; ?>
<td>
<?php //whatever you want to put in your column goes here; ?>
</td>
<?php $i++; endforeach; ?>
</tr>
</table>
If you want the table to be four rows across, just change the "if ($i % 2 == 0)" to "if ($i % 4 == 0)".

Basic php form help (2)

This is an extension of a question I had yesterday.
I am trying to make a little php calculator that will show how much people can save on their phone bills if they switch to VoIP, and how much they can save with each service.
I have a form that will spit out the right amount for a monthly bill here:
http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit
But now I need to integrate that with some other data and put in a table. The prices for each of the different services are in another file called "values.php". The values are:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";
I want each of the seven rows of the table to have one of the offercalcsavings values subtracted from the monthlybill value.
The php code currently looks like this:
<?php $monthlybill = $_GET['monthlybill']; ?>
Your monthly bill was <?php echo "$monthlybill"; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!=""){
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td ><span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="http://www.nextadvisor.com'.$offerstar.'" alt="" />
</span><br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>
Review
</div> </td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
I want each of the seven rows of the
table to have one of the
offercalcsavings values subtracted
from the monthlybill value.
You need to do the math somewhere. Presumably you would do this before the echo statement where you output your row.
$offerwithsavings = $monthlybill - $offercalcsavings
Then just be sure to include it in your table somewhere.
<td >'.$offerwithsavings.'</td>
The real prescription for this code is an array and foreach() loop, but I thought I'd keep my answer simple for now. Arrays and foreach() loops are very powerful and relatively quick and easy to learn. You would do well to give them some in depth study.
You can include the values.php file like this:
include 'path/to/values.php';
If you put the values in values.php in an array you can easily loop through them using a foreach loop:
in values.php;
$values['1calsavings'] = 24.99;
$values['2calsavings'] = 20.00;
etc;
Then in the other file:
include 'path/to/values.php';
foreach($values as $name => $amount){
echo '<some_markup>';
echo "$name $amount";
echo '</some_markup>';
}
Egads.
Use arrays!
So:
$offercalcsavings[0] = "24.99";
$offercalcsavings[1] = "20.00";
etc. etc.
Then, you're looping the output really:
for($i = 0; $i < CONSTANT_THAT_EQUALS_7; $i++) {
echo "<html bits>";
echo $offercalcsavings[$i];
echo $offerlink[$i];
etc. etc.
}

Categories