Formatting dynamically generated checkboxes? - php

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.

Related

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

Status of questions in an online exam with 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");
};
});
});

List won't show up correctly when integrated with PHP

I made a database that has a points system, and to convert this to ranks I planned on showing the data from the database from highest to lowest, then using a list to display it with numbers. However, when displaying it as a list it just shows up as:
1.
1.
1.
1.
1.
1.
(going on until it reaches 10)
Which is not how it should act, I have tried changing the stuff up around the code and the results have only worsened. Can anyone help?
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td><ol><li style='list-style-type:decimal;'> ".$row['points']."</li></ol></td> <td> </td>
<td> </td>
<td> </td></tr>";
}
?>
As mentioned in the comments remove your
<td>
tags or do a table instead of an ordered list and better use ' instead of " for surrounding html tags with attributes in it and use the " for the attribute values, because then you get valid html and no problems in php.
Correct code for list would be:
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
echo "<ol>";
while($row = mysqli_fetch_array($mw2ranks))
{
echo '<li style="list-style-type:decimal;"> '.$row['points'].'</li>';
}
?>
</ol>
For a table you could use the following:
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
?>
<table>
<tr>
<?php
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td> ".$row['points']."</td>
}
?>
</tr>
</table
Your HTML is broken. You can't include the <td> elements inside your <ol>. You need to create a structure like this:
<td>
<ol>
<li> Some stuff </li>
<li> Some stuff </li>
<li> Some stuff </li>
<li> Some stuff </li>
</ol>
</td>
Grouping your <li> elements like this will allow the numbering to work, but obviously it will mess up your table layout. Your alternative is to generate the numbering in your PHP script and not use the ordered list structure at all.
you could use this
<table>
<tr>
<?php
$i=1;
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td> ".$i."</td>";
echo "<td> ".$row['points']."</td>";
$i++;
}
?>
</tr>

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

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

Categories