I am creating a php page which shows mcq questions with options and answers from database. Here i am using a html button for show/hide answers. but its working only for the first question but while clicking on other questions it works for the first question only. kindly help
<script>
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
<?php
while($row = mysqli_fetch_array($result))
{
echo "<h4 style=font-weight:bold; text-align:justify>";
echo "Q $row[slno]) $row[question]";
echo "</h4><br>";
echo "<ul class=list-group>";
echo "<li class=list-group-item>1) $row[option1]</li>";
echo "<li class=list-group-item>2) $row[option2]</li>";
echo "<li class=list-group-item>3) $row[option3]</li>";
echo "<li class=list-group-item>4) $row[option4]</li>";
echo "<li class=list-group-item>5) $row[option5]</li>";
echo "<div id=myDIV style=display:none><li class=list-group-item><b>Correct Option : ($row[correct_option])</b></div></li>";
echo "<li class=list-group-item><button onclick=myFunction()>View Answer</button></li>";
echo "</ul>";
echo "<hr color=#FF0000 size=5 width=100%>";
}
?>
Any suggestions...
Try this..
<script>
function myFunction(num) {
var x = document.getElementById('myDIV'+num);
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
<?php
$num=1;
while($row = mysqli_fetch_array($result))
{
echo "<h4 style=font-weight:bold; text-align:justify>";
echo "Q $row[slno]) $row[question]";
echo "</h4><br>";
echo "<ul class=list-group>";
echo "<li class=list-group-item>1) $row[option1]</li>";
echo "<li class=list-group-item>2) $row[option2]</li>";
echo "<li class=list-group-item>3) $row[option3]</li>";
echo "<li class=list-group-item>4) $row[option4]</li>";
echo "<li class=list-group-item>5) $row[option5]</li>";
echo "<div id=myDIV".$num." style=display:none><li class=list-group-item><b>Correct Option : ($row[correct_option])</b></div></li>";
echo "<li class=list-group-item><button onclick=myFunction('".$num."')>View Answer</button></li>";
echo "</ul>";
echo "<hr color=#FF0000 size=5 width=100%>";
$num++;
}
?>
Try this:
Your Id must be unique for each records.
<script>
function myFunction(elem) {
var x = document.getElementById('myDIV'+elem);
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
<?php
while($row = mysqli_fetch_array($result))
{
echo "<h4 style=font-weight:bold; text-align:justify>";
echo "Q $row[slno]) $row[question]";
echo "</h4><br>";
echo "<ul class=list-group>";
echo "<li class=list-group-item>1) $row[option1]</li>";
echo "<li class=list-group-item>2) $row[option2]</li>";
echo "<li class=list-group-item>3) $row[option3]</li>";
echo "<li class=list-group-item>4) $row[option4]</li>";
echo "<li class=list-group-item>5) $row[option5]</li>";
echo "<div id=myDIV'".$row['slno']."' style=display:none><li class=list-group-item><b>Correct Option : ($row[correct_option])</b></div></li>";
echo "<li class=list-group-item><button onclick=myFunction('".$row['slno']."')>View Answer</button></li>";
echo "</ul>";
echo "<hr color=#FF0000 size=5 width=100%>";
}
?>
Related
This is my PHP script to generate the table:
$products = get_db( "SELECT * FROM `products` ORDER BY Data desc" );
echo '<form id="productsEdit" action="actions.php" method="get" />';
echo "<table id='products'>";
echo "<thead>";
echo "<tr id='filter_table'>";
echo "<th class='id'>Cod. prodotto<br><input type='text' class='id' /></th>";
echo "<th class='articolo'>Articolo<br><input type='text' class='articolo' /></th>";
echo "<th class='fornitore'>Fornitore<br><input type='text' class='fornitore' /></th>";
echo "<th class='nome'>Nome<br><input type='text' class='nome' /></th>";
echo "<th class='taglia'>Taglia<br><input type='text' class='taglia' /></th>";
echo "<th class='colore'>Colore<br><input type='text' class='colore' /></th>";
echo "<th class='prezzo_acquisto'>Prezzo acquisto<br><input type='text' class='prezzo_acquisto' /></th>";
echo "<th class='prezzo_vendita'>Prezzo vendita<br><input type='text' class='prezzo_vendita' /></th>";
echo "<th class='data'>Data<br><input type='text' class='data' /></th>";
echo "<th class='q'>Qta<br><input type='text' class='q' /></th>";
echo "<th class='qA'>QtaA<br><input type='text' class='qA' /></th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($products as $product) {
echo "<tr class='prod_". $product['id'] ."'>";
echo "<td class='id'>". $product['id'] ."</td>";
echo "<td class='articolo'>". $product['articolo'] ."</td>";
echo "<td class='fornitore'>". $product['fornitore'] ."</td>";
echo "<td class='nome'>". $product['nome'] ."</td>";
echo "<td class='taglia'>". $product['taglia'] ."</td>";
echo "<td class='colore'>". $product['colore'] ."</td>";
echo "<td class='prezzo_acquisto'>". $product['prezzo_acquisto'] ."</td>";
echo "<td class='prezzo_vendita'>". $product['prezzo_vendita'] ."</td>";
echo "<td class='data'>". $product['data'] ."</td>";
echo "<td class='q'>". $product['quantita'] ."</td>";
echo "<td class='qA'>". $product['qta_acquisto'] ."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</form>";
My jQuery script:
$('#filter_table input').live("keyup", function() {
var searchStr = $(this).val();
var column = $(this).attr("class");
if (searchStr.length > 0) {
$('tbody > tr').hide();
$('td.'+ column +':containsNC('+ searchStr +')').each(function(){
$(this).parent("tr").show();
});
} else {
$('tbody > tr').show();
}
});
The filter work only for a column, but it doesn't work when I apply filter on more columns in the same time, such as 'AND' condition between fields of different columns.
Can someone help me?
var $rows = $('tbody > tr'),
$filters = $('#filter_table input'),
cache, $m, len, cls;
$filters.on("keyup", function () {
$m = $filters.filter(function () {
return $.trim(this.value).length > 0;
}), len = $m.length;
if (len === 0) return $rows.show();
cache = {};
cls = '.' + $m.map(function () {
cache[this.className] = $.trim(this.value);
return this.className;
}).get().join(',.');
$rows.hide().filter(function () {
return $('td', this).filter(cls).filter(function () {
return this.textContent.indexOf(cache[this.className]) > -1;
}).length === len;
}).show();
});
http://jsfiddle.net/k47dkruh/
Note that live method was deprecated in jQuery 1.7 and removed in jQuery 1.9, in the above example I have used the on method. Based on the posed code there is no need to even use event delegation technique, if you are using one of the older versions of jQuery that doesn't have on method use the click method. If you really need to delegate the event use live or the delegate method.
The table is called bookings. I'm trying to echo rows from the table, but I'm getting errors in some places according to user_id. Dreamweaver tells me the last few lines have errors, but I'm not sure what's the problem.
<?php
require 'connect.php';
include("auth.php");
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id =". $uid;
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>"Room_name"</th>
<th>"booking_date"</th>
<th>"period_name"</th>
<th>"booking_id"</th>
<th>"Cancel Booking"</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo "</tr>";
echo "</table>";
}
else echo '<p>You have no bookings.</p>';
I have made some change
And here is the code-
require 'connect.php';
include 'auth.php';
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id ='". $uid."'";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>Room_name</th>
<th>booking_date</th>
<th>period_name</th>
<th>booking_id</th>
<th>Cancel Booking</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo '</tr>';
}
echo '</table>';
} else echo '<p>You have no bookings</p>';
I want to add a dropdown at checkout page which has option of pick up time and pickupdate. currently I added shipping methods in dropdown I want to add pick up date and time instead of it.
<?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
<form id="co-shipping-method-form-drop" action="<?php echo $this->getUrl('checkout/cart/estimateUpdatePost') ?>">
<select id='drop_estimate_method' name="estimate_method">
<option value='-1'>Please Select Shipping Method</option>
<?php
foreach ($_shippingRateGroups as $code => $_rates) {
$str = $this->getCarrierName($code);
foreach ($_rates as $_rate){
$str2 = $str . ' - ' . $_rate->getMethodTitle();
$_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax());
$_incl = $this->getShippingPrice($_rate->getPrice(), true);
$str2 .= ' ['. $_excl;
if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl) {
$str2 .= ' '. $this->__('Incl. Tax'); echo $_incl;
}
$str2 .= '] ';
?>
<option <?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' selected="selected"' ?> value="<?php echo $this->htmlEscape($_rate->getCode()) ?>"><?php echo $str2;?></option>
<?php }
} ?>
</select>
</form>
<script type="text/javascript">
document.observe("dom:loaded", function() {
$('drop_estimate_method').observe('change',function(){
if($('drop_estimate_method').getValue() == -1){
alert('Please Select Shipping Method');
}else{
$('co-shipping-method-form-drop').submit();
}
});
});
</script>
<?php endif; ?>
The problem is that all images are displayed with opacity: 0.4. I need different style to each row that value from number column is less than 100.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$progres = $row['number'];
$num_rows++;
echo "$progres <br/>";
echo "<div class='right_achiev'>";
echo '<div id="box1" class="box">';
echo '<span class="caption simple-caption">';
echo '<p class="sq1">'.$row['name'].'</p>';
echo '<p class="unlok">UNLOCKED:<br/> 2014-01-09 16:25 </p>';
echo '</span>';
if ($progres < 100)
{
echo "<img class='achiev_icon' src='".$row['icon']."'/>";
}
else
{
echo "<img class='achiev_icon' style='opacity:0.4' src='".$row['icon']."'/>";
}
echo "</div>";
echo "</div>";
}
you can try to use this code
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$progres = intval($row['number']);
$num_rows++;
echo "$progres <br/>";
echo "<div class='right_achiev'>";
echo '<div id="box1" class="box">';
echo '<span class="caption simple-caption">';
echo '<p class="sq1">'.$row['name'].'</p>';
echo '<p class="unlok">UNLOCKED:<br/> 2014-01-09 16:25 </p>';
echo '</span>';
if ($progres < 100)
{
echo "<img class='achiev_icon' src='".$row['icon']."'/>";
}
else
{
echo "<img class='achiev_icon' style='opacity:0.4' src='".$row['icon']."'/>";
}
echo "</div>";
echo "</div>";
}
I've made a script for movies but i have an issue.
The home of script is contain code like this :
<div class="box">
<div id="movie"></div><div id="movie"></div><div id="movie"></div>
</div>
I've do a mysql query but i don't know how to echo just 3 record per div .
Im using this code for mysql query :
$query=mysql_query("select * from movies where id=$id");
while($row=mysql_fetch_assoc($query))
{
echo $row['name'];
}
You will need something like this I think
$query = mysql_query("select * from movies");
$result = array();
while($r = mysql_fetch_assoc($query)) {
$result[$r['id']] = array($r['name'],$r['thumb']);
}
$i = 0;
foreach($result as $id => $data){
if($i == 0)
{
echo "<div class=\"box\">";
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 1)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 2)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
echo "</div>";
$i = 0;
}
}
Multiple elements with the same id? Not a good idea.
You didn't say what you want to happen if you're query returns less then 3 rows.
You might try:
...
$x=0;
while($row=mysql_fetch_assoc($query) && ++$x<=3) {
echo $row['name'];
}