I want to make it stop looping, but I dont know what's wrong. I want it to be like..
Clicking the button then shows the text, and keep repeating like that in one page.
I have the code tht goes like this:
<?php
include "koneksi.php";
$query = mysqli_query($connection,"SELECT * FROM buatsoal_db ORDER BY ID DESC");
$id = mysqli_query($connection, "select 'ID' from buatsoal_db");
?>
<?php
do{ ?>
<form method= 'post'>
<input name='next' type='submit' id='next' value='next'>
</form>
<?php if(isset($_POST['next'])){ ?>
<table width="637" border="0" cellspacing="1" cellpadding="2">
<tr>
<th width="297" scope="col">
<?php $row = mysqli_fetch_array ($query);
echo $row['SOALTXT']; ?>
</tr>
</table>
<?php }
?>
<?php }
while ($id = 'S01');
echo "sudah selesai";
?>
I dont know what's wrong, it keeps looping like this :
first_executes_looping non stops
and the button next shows up all together, and when I clicked it, the txt shows up at the same time too. I want it to shows up one by one, and keeep it repeat until the id="s01"from the txt is over.
There is problem with your while condition. single = is used to assign values and == is used to compare. So here you have to use == to compare. Try below code.
<?php
include "koneksi.php";
$query = mysqli_query($connection,"SELECT * FROM buatsoal_db ORDER BY ID DESC");
$id = mysqli_query($connection, "select 'ID' from buatsoal_db");
?>
<?php
do{ ?>
<form method= 'post'>
<input name='next' type='submit' id='next' value='next'>
</form>
<?php if(isset($_POST['next'])){ ?>
<table width="637" border="0" cellspacing="1" cellpadding="2">
<tr>
<th width="297" scope="col">
<?php $row = mysqli_fetch_array ($query);
echo $row['SOALTXT']; ?>
</tr>
</table>
<?php }
?>
<?php }
while ($id == 'S01');
echo "sudah selesai";
?>
Just use the comparison statement in do while loop like:
do{
//your code block here
}while($id == 'S01');
Issue is in while statement.
what you have to do is:
do {
...
}
while($id == 'S01');
== is the solution
Related
I'm working on a PHP script that uses delete and I only want to delete 1 row. But everytime I tried to delete 1 row, the SQL executes but deletes the entire rows in the table.
The following is my PHP script
<table>
<tr>
<th>No.</th>
<th>Publisher Code</th>
<th>Publisher Name</th>
<th>City</th>
<th> </th>
</tr>
<!-- PHP FETCH/RETRIEVE FROM DB -->
<?php
include_once 'dbconnect.php';
$sql = mysqli_query($conn, "SELECT * FROM tbl_publisher");
$ctr = 1;
$record = mysqli_num_rows($sql);
if ($record > 0) {
while ($record = mysqli_fetch_array($sql)) {
?>
<tr>
<td> <?php echo $ctr++ ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_CODE']; ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_NAME']; ?> </td>
<td> <?php echo $record['CITY']; ?> </td>
<td id="actions">
<center>
Delete
</center>
</td>
</tr>
<!-- closing tag for php script -->
<?php
}
}
?>
<!-- -->
</table>
and below is my delete.php
if(isset($_GET['del-pub']))
{
$row = intval($_GET['del-pub']);
$sql = mysqli_query($conn,"DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;");
if ($sql) {
header("Location: publisher.php");
}
else {
echo "<script>alert('Publisher was not deleted.');</script>";
header("Location: publisher.php");
}
}
I want to know how to delete only the 1 row. But it keeps deleting the entire rows in the table.
After dumping the contents of $sql, I found out that my sql syntax is the culprit.
Since TBL_PUBLISHER_CODE uses char as ID. Instead of doing "="
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;
I used
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE LIKE '".$row."'
I have a problem with my first attempt at making a foreach loop.
My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table.
My code looks like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
It's being displayed like so on the page:
/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK
Hope you can help me:)
<?php
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$counter=0;
while($row= mysql_fetch_assoc($result))
{
$assoc_query[$counter] = $row;
$counter++;
}
foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $value['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $value['name']; ?> </div>
</td>
</tr>
<?php } ?>
Instead of foreach you will want a while. What you are doing here is looping the elements of array $assoc_query.
A while loop will get the next result set to be used.
while($assoc_query = mysql_fetch_assoc($result)){
//Do your thing
}
Also please consider updating from mysql_ to mysqli_ or PDO. What you are using is depreciated and there is added security with the newer ones.
this can't be done with foreach loop unless you gonna show 1 user's information only using limit Clause or Where Clause otherwise that it's useless
instead you need a while loop like this
<?php while ($assoc_query = mysql_fetch_assoc($result)) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
You should do it like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
That should work. But as said before you shouldn't code like this. Use mysqli or PDO like nerdlyist says.
#Nerdlyist
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
Insert Is Fine But if I select this value from database All Values are fine but single values fetch array problem I don't know how to solve this task. Please Update this code asap.
This Is Insert Code .....
<?php
if(isset($_POST['sendmessage'])){
$entermessage = $_POST['teachermessage'];
$checkbox_user = $_POST['usernameallcheckbx'];
$arr = implode(',',$checkbox_user);
//$arr2 = explode(',',$arr);
$insert = mysql_query("INSERT into usermessages(fromteacher,toparent,messages) VALUES('".$_SESSION['username']."','$arr','$entermessage')");
if($insert == 1){
echo "<h1>successful</h1>";
}
else{
echo mysql_error();
}
}
?>
<form method="post">
<div style="float:left; width:450px;"><br/><br/>Message: <br/>
<textarea style="width:400px; height:300px;" name="teachermessage"></textarea><br/>
<input type="submit" value="Send" name="sendmessage" /></div>
<div style="float:left; with:200px; padding-top:55px;">
<table>
<tr>
<?php
$select_query1 = mysql_query("SELECT * FROM register_user WHERE teacher='$teachername'");
while($chckbx=mysql_fetch_array($select_query1))
{
?>
<td><?php echo "<input type='checkbox' name='usernameallcheckbx[]' value=". $chckbx['userid']." />"; ?></td>
</tr>
<tr>
<td><?php echo $chckbx['parent_fname']." ".$chckbx['parent_lname']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</form>
And This Is Select Code....
<h2>Messages</h2>
<?php
$select_query3 = mysql_query("SELECT * FROM usermessages");
$fetch= mysql_fetch_array($select_query3);
$data=$fetch['toparent'];
$arr=explode(',',$data);
//$userids=explode(',',$data);
$sessionshow=$_SESSION['userid'];
$userids=in_array("$sessionshow",$arr);
$select_query2 = mysql_query("SELECT * FROM usermessages WHERE toparent in ('$userids')='".$_SESSION['userid']."'");
?>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Teacher Name</th>
<th>Message</th>
</tr>
<?php
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo "<tr>";
echo "<td>".$fetch_data=$fetch_name2['fromteacher']."</td>";
echo "<td>".$fetch_data=$fetch_name2['messages']."</td>";
echo "</tr>";
}
?>
</table>
I have only put this as an answer to show some incorrect code - remove fetch data from your loop as it doesn't do anything
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo "<tr>";
echo "<td>".$fetch_name2['fromteacher']."</td>";
echo "<td>".$fetch_name2['messages']."</td>";
echo "</tr>";
}
even better is use of a HEREDOC:
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo <<<EOF
<tr>
<td>{$fetch_name2['fromteacher']}</td>
<td>{$fetch_name2['messages']}</td>
</tr>
EOF;
}
I really can't figure out what you're trying to do, but maybe this is the query you want:
SELECT * FROM usermessages WHERE FIND_IN_SET('$sessionshow', toparent)
FIND_IN_SET(str, strlist) searches the comma-separated list in strlist for an element that equals str, and returns the position in the list; if it's not found it returns 0, which counts as false.
I can't see any purpose to any of the code that uses $select_query3.
I am building an application that gives user the opportunity to click on the day of the calendar and when there is an activity registered on that day, it shows a table with the activity registered...my problem is that when there is no activity on that day, I wont it to return the string : There is no activity.. I try to make it using if($nrofrows>0)like below but all the time it returns me the string :there is not activity, even I have activity on that day.
Please can you help me ? where is my error? Thanks in advance...
<body>
<?php
mysql_connect("127.0.0.1","root","") or die("Smund te lidhet me serverin");
mysql_select_db("axhenda") or die("Kjo databaze nuk u gjet");
session_start();
$perdoruesi=$_SESSION['user_id'];
$result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]' and Perd_Id='$perdoruesi'");
$nrofrows= mysql_num_rows($result);
if($nrofrows>0)
{
?>
<div class="title"> Aktivitetet per daten <?php print ("$_POST[dataoutput]"); ?></div>
<form name="form1" method="post" action="delete.php">
<table >
<th>
<th ><strong>Emri </strong></th>
<th ><strong>Pershkrimi </strong></th>
<th><strong>Ora</strong></th>
</th>
<?php
while ($row=mysql_fetch_array($result)) {
?>
<tr>
<td ><input name="checkbox[]" type="checkbox" value="<?php echo $row['Id_Akt']; ?>"></td>
<td style="font-size:0.9em"><?php echo $row['Emri']; ?></td>
<td ><?php echo $row['Pershkrimi']; ?></td>
<td><?php echo $row['Ora']; ?></td>
</tr>
<?php
}
?>
</table>
<input class="button" name="delete" type="submit" value="Delete" style="margin-left:40%; margin-top:100px; width:15%">
</form>
<?php
}
else {
echo "<span id='errorformat'>There is no activity on this day!<span>";}
?>
</body>
</html>
Might be a problem with the query you can add this to see:
$result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]' and Perd_Id='$perdoruesi'") or die ("query error" .mysql_error());
mysql_num_rows($result) returns false when there is an error so you might want to add this:
if($nrofrows && $nrofrows>0)
try this
if ( mysql_num_rows($result) > 0 ) {
}
I have a html table where i want to echo content out of a database table:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
</td>
<td>
<?php
$ergebnis = mysql_query("SELECT Test2 FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test2;
}
</td>
</tr>
</tbody>
</table>
Is there a way to cut short the php code, because it´s a bit too long to wirte in every
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
If your example really is what you are doing you might want to rather get all data in a single query:
<?php
$ergebnis = mysql_query("SELECT Test, Test2 FROM testtable");
$recordset = mysql_fetch_array($ergebnis, MYSQL_ASSOC);
?>
<table style="width: 100%;">
<tbody>
<tr>
<?php foreach (array('Test', 'Test2') as $column) { ?>
<td>
<?php foreach ($recordset as $record) { ?>
<?php echo htmlspecialchars($record[$column], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
<td>
<?php } ?>
</tr>
</tbody>
</table>
Also note that the mysql_* functions has been deprecated and will be removed from the language soon. If you want to make sure your code doesn't start throwing notices or even will stop functioning in the future you might want to start using either mysqli_* or PDO.
http://php.net/manual/en/mysqlinfo.api.choosing.php
Also note that I have added htmlspecialchars() to prevent possible XSS attacks.
Well, you can simply do it by having a function:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}
and calling it like this:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$a = 'Test';
query($a);
?>
</td>
<td>
<?php
$a = 'Test2';
query($a);
?>
</td>
</tr>
</tbody>
</table>
Create a function out of it. And use wherever you want.
You can make a function like this:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}