can i use while loop inside another while loop in php - php

I'm creating a comment system in my project. I want each posted question to have a comment. I was able to post the comment, but I am having a problem displaying all comment to it's respective answers. I was able to display only one row but not the row of the comment. I try to use a while loop nested inside the while that echo's each question, but it hangs. When I use if it only displays the first row of the comment of each question.
So my question is how can I display them all?
<div class="answer">
<?php
include 'db.php';
$sql = "select * from answers where questionrid IN(select id from question where id='$qid')";
$result = mysqli_query($con,$sql);
if($result){
while($ro = mysqli_fetch_assoc($result)){
?>
<div class="a_view">
<pre>
<?php
echo $ro["answer"];
?>
</pre>
</div>
<div class="ans_comment">
<?php
if($ro["id"]){
$id = $ro["id"];
$sqli = "SELECT * FROM comments WHERE answerid='$id'";
$query = mysqli_query($con,$sqli);
$row = mysqli_fetch_assoc($query);
$num = mysqli_num_rows($query);
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
}
?>
</div>
<div class="add"><div class="coment">add a comment</div> <div id="coment">
<form class="cform" method="post" action="acomment.php">
<textarea type="text" name="comment" class="tcomment" placeholder="add your comment here,your is
required to give correction or more information about the problem"></textarea><br><br>
<input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid">
<input type="submit" value="Post your comment">
</form>
</div></div>
<?php
}
}else{
echo "no record";
}
?>
<?php
$con->close();
?>
This is the section that made it hang
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
when I use if, it only echos one row.

Instead of
while($row){
do it just like you're doing in the while loop above
while($row = mysqli_fetch_assoc($query)){
The way you have it now, $row is never changing, and therefore always evaluates to true, leaving you stuck in your loop.

Related

i want to add commenting to each article, so inside my"foreach"cycle I added commenting to each article, but "set a comment" function runs to all art

i want to add commenting to each article, so inside my"foreach"cycle I added commenting to each article, but "set a comment" function runs to all art
thats the code for making an article window
<?php $articles_qr = mysqli_query($connection, "SELECT * FROM `articles` ");
$articles = array();
while ( $art = mysqli_fetch_assoc($articles_qr))
{
$articles[] = $art;
}
?>
<?php foreach ($articles as $art)
{
?>
<section>
<div class="containerstuff">
<div class="stuffpic">
<img src="<?php echo "../static/imagespages/",$art['image'] ?>" class="pico">
</div>
<div class="article">
<h1><?php
echo $art['title']
?>
</h1>
<?php
echo $art['text'];
echo $art['id']
?>
</div>
</div>
<div class="scrollmenu">
<?php include "../includes/comments.php";?>
</section>
<?php
} ?>
thats the code comments window
<?php
date_default_timezone_set(timezone_identifier);
include_once '../comments.ink.php'
?>
<div class="containercom">
<img src="#" class="commpic">
<p class="comment"></p>
</div>
<div class="blockcom">
<form class='form' method='POST' action="<?php echo setComments($connection)?>">
<div class='form__group'>
<input type='hidden' name='page_id' value="<?php echo $art['id']?>" >
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='pubdate' value="<?php echo date('Y-m-d H:i:s')?>" >
<textarea name='text' class='form__control' placeholder ='Введите комментарий...' required=''></textarea>
</div>
<div class='form__group'>
<input type='submit' class='form__control2' name='commentSubmit'></input>
</div>
</div>
</form>
and thats the code for INSERT function
<?php
static $firstTime = true;
function setComments($connection) {
if(isset($_POST['commentSubmit'])){
$idcom = $_POST['page_id'];
$uid = $_POST['uid'];
$pubdate = $_POST['pubdate'];
$text = $_POST['text'];
$sql =
"INSERT INTO `comments` (page_id, uid, pubdate, text)
VALUES ('$idcom', '$uid', '$pubdate', '$text')";
$result = $connection->query($sql);
$firstTime = false;
}
}
so how can i make insert only for 1 article (so when i add it now, there are appears as many comments as many articles i have in database)
I think you should use ajax to append a new comment which is the widely used solution, the way u r doing will become difficult to handle for you.
I haven't found the solution, so I just selected another way.
For each article I placed a button to post a comment which will send user to the page with this article and for this button for "href" I wrote php code (href = "comments.php?id=<?php echo $art['id']"?>) and for this page I use $_GET to select articles only for this id. Then I just placed there comments-function that I wrote so it works alright now because function works only for 1 argument

Warning: Invalid argument supplied for foreach() HTML embedded

I have made a basic search engine and I try, to fetch the results, on the same page; moreover, the results have been retrieved and stored in the associative array, but the embedded code in HTML shows only one record, of the results. `
<?php
require('Configuration/config.php');
require('Configuration/db.php');
//If the user clicks, on the button search, the execute the query
if (isset($_POST['search_btn'])) {
$search_query = $_POST['search'];
//$search_query = htmlspecialchars($_POST['search']);
//Create the query.
$query = "SELECT * FROM The_primary_arkivum WHERE
Name = '$search_query' OR
Address = '$search_query' OR
Category = '$search_query' OR
Country = '$search_query' OR
State = '$search_query'";
//Get the results.
$results = mysqli_query($conn, $query);
//Fetch the data, of the result, to an array.
$search_results = mysqli_fetch_all($results, MYSQLI_ASSOC);
//var_dump($search_results);
//var_dump($search_query);
//Free result
mysqli_free_result($results);
//Close the connection
mysqli_close($conn);
}
?>
<?php include('included/header.php'); ?>
<body>
<div class = "header">
<h2>Search</h2>
</div>
<form method="post" action="search_index.php">
<div class="input-group">
<label>Search</label>
<input type="text" name="search" value="<?php echo $search; ?>">
</div>
<div class="input-group">
<button type="submit" class="btn" name="search_btn">Search</button>
</div>
<?php foreach($search_results as $search_result) : ?>
<div class="mySlides fade">
<?php echo $search_result['Name']?>
<?php echo $search_result['Address']?>
<?php echo $search_result['Country']?>
</div>
<?php endforeach; ?>
</form>
<?php include('included/footer.php'); ?>
`
Your echo statements in your form do not have ending semicolons ;. Try starting there.
<?php echo $search_result['Name'];?>
<?php echo $search_result['Address'];?>
<?php echo $search_result['Country'];?>

MySQL not all results are shown

i have made i small membersearch for my site that looks for first name oder second name in my database. all members are inserted by the same way but i get not always a result back. if no member with a typed name is found than i get a message. that seams to work, but some times i get no message or if there are more than one results i get not all of them.
here is my code i use.
<form id="tfnewsearch" method="POST" action="admin_search_member.php">
<input id="tfq" class="tftextinput4" name="q" size="21" maxlength="240" value="Mitglied suchen..." />
<input type="submit" name="startsuche" value=" " id="tfbutton4">
</form>
and that's my query
if(isset($_POST['startsuche'])) {
$sql = "SELECT * FROM Mitglieder WHERE vorname LIKE '%".mysql_real_escape_string(trim($_POST['q']))."%' OR nachname LIKE '%".mysql_real_escape_string(trim($_POST['q']))."%'";
$result = mysql_query($sql) OR die("<pre>\n".$sql." </pre>\n".mysql_error());
$row = mysql_fetch_array($result);
}
if (mysql_num_rows($result) == 0) {
?>
<div id="body_box_tabs">
<div class="tabcontents">
<div id="view1">
<p style="color: #003137; font-weight: bold;">Das gesuchte Mitglied existiert nicht!</p>
<input class="button-link" type="submit" value="Zurück"/>
</div>
</div>
</div>
<?php
} else {
echo $_POST['q'];
?>
<div id="body_box_tabs">
<div class="tabcontents">
<div id="view1">
<?php
while($row = mysql_fetch_array($result)) {
echo "show me the found member";
}
}
?>
I hope some one can help me with this.
You're basically fetching and then discarding the first row of the query result. This will cause the "does not exist" message to be shown when there are no rows, will cause no output when there's one row returned, and will cause (n-1) outputs when n>1 rows are found. You will never see the first row of the result this way.
Either remove the first $row = mysql_fetch_array($result);, or change your loop:
do {
echo "show me the found member";
} while($row = mysql_fetch_array($result));
In this code block:
<?php
while($row = mysql_fetch_array($result)) {
echo "show me the found member";
}
}
?>
Do this:
<?php
while($row = mysql_fetch_array($result)) {
echo $row['field_name'];
}
}
?>
That should do the trick...

Approve row through a form - getting blank page

I want to show a list of all entries where the column "approved" is "no" and then place a button next to it that when clicked will change the "approved" to a "yes". I ran this through code checker and fixed a few issues with brackets and such. It now tells me there are no errors so something else is just not working with this. It is probably something small that I am missing (I hope). Can anyone help me find/understand what part of this is incorrect... and/or if there is a better way to achieve what I'm wanting?
<?php
//select the database to write to
$unapprovedsires = mysql_query("SELECT * FROM nominatedsires WHERE approved = 'no'");
//While loop to cycle through the rows
while($row = mysql_fetch_assoc($unapprovedsires)){
$sirename = $row['sirename'];
echo $sirename;}
?>
<ul class="admin-fields">
<?php
foreach($row as $field){
if(empty($field)){
echo "....";
}
print '<li>' .$field.' </li>';
}//End For Each Loop
//print $sirename;
?>
</ul>
<p>
<?php
if(isset($_POST['approve'])){
mysql_query("UPDATE nominatedsires SET approved = 'yes' WHERE sirename = '.$sirename.'") or die ("Something went wrong");
}
?>
<ul>
<li>
<form method="post">
<input type="hidden" name="sirename" value="$sirename" />
<button name="approve" id="approve" type="submit">Approve Sire</button>
</form>
</li>
</ul>
As I mentioned as a comment, you need to wrap $sirename in PHP tags with an echo statement. You are also not passing $_POST['sirename'] into your script. It otherwise defaults to the original $sirename from your mysql_fetch_assoc().
Warning: the way you have your script set up, you're vulnerable to injection attacks. This is just an example to show you how to pass in variables. See: How can I prevent SQL injection in PHP?
<?php
//select the database to write to
$unapprovedsires = mysql_query("SELECT * FROM nominatedsires WHERE approved = 'no'");
//While loop to cycle through the rows
while($row = mysql_fetch_assoc($unapprovedsires)){
$sirename = $row['sirename'];
echo $sirename;}
?>
<ul class="admin-fields">
<?php
while($row = mysql_fetch_assoc($unapprovedsires)){
if(empty($row['sirename']))
echo "....";
else
print '<li>' .$row['sirename'].' </li>';
}
//print $sirename;
?>
</ul>
<p>
<?php
if(isset($_POST['approve'])){
$sirename = mysql_real_escape_string($_POST['sirename']);
mysql_query("UPDATE nominatedsires SET approved = 'yes' WHERE sirename = '$sirename'") or die ("Something went wrong");
}
?>
<ul>
<li>
<form method="post" method="<?php echo $_SERVER[PHP_SELF]; ?>">
<input type="hidden" name="sirename" value="<?php echo $sirename; ?>" />
<input type="submit" name="approve" id="approve" value="Approve Sire" />
</form>
</li>
</ul>

Resource id #6 error in PHP with MySQL

I am this is for a polling system I am making, this code shows the user a list of questions they can pick from:
<div class="main_questions">
<p class="style1 style2"><strong>Select Your Question</strong></p>
<p class="style1">
<form action="vote_list.php" method="post" name="form1" class="style1">
<?php
$sql = "SELECT DISTINCT (question_tba) FROM question ORDER BY answer_id DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
?>
<p>
<?php echo $row['question_tba']; ?>
<input type="radio" name="questions" value="<?php echo $row['question_tba']; ?>">
</p>
<?php
}
?>
<input type="submit" name="submit" value="Vote">
</p>
</div>
This code should then post the chosen question to this page which allows them to cast a vote:
<?php
include('core/initialise.inc.php');
$q = $_POST['question_tba'];
if (isset($_GET['vote'], $_GET['id'])){
add_vote($_GET['id'], $_GET['vote'], $q);
echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=vote_logged.php\">";
}
?>
</div>
<div class="main_questions">
<p class="style1 style2"><strong>Place Your Vote!</strong></p><?php
foreach (get_answers($q) as $id => $answer){
?>
<p>
<?php echo $answer; ?>
Vote
</p>
<?php
}
?>
</div>
Then when they click on an answer to vote on the functions on this page should increment the chosen answers vote by 1 using these functions:
<?php
function get_answers($q){
$q = $q;
$sql = "SELECT answer_id, answer FROM question WHERE question_tba = '$q'";
$result = mysql_query($sql);
echo "$result";
$answers = array();
while (($row = mysql_fetch_assoc($result)) or die(mysql_error()))
{
$answers[$row['answer_id']] = $row['answer'];
}
return $answers;
}
function add_vote($answer_id, $vote, $q2){
$q2 = $q2;
$answer_id = (int)$answer_id;
$vote = '+';
$sql = "UPDATE question SET answer_votes = answer_votes $vote 1 WHERE question_tba = $q2 AND answer_id = $answer_id";
mysql_query($sql);
}
?>
However, my problem is that when I click on the question I would like to vote on, instead of displaying the answers that I can choose to vote from, it just displays Resource id #6. Can anyone tell me what is wrong with my code?
$result is a resource returned by your mysql_query() call, not an actual row object/array. In the same way that you are using mysql_fetch_assoc() in other areas of your code to extract data, you will need to do this prior to your echo if you want to display the data.
Okay, got this sorted out. Turns out, when posting a radio button value you have to use the name="" instead of value="" pretty easy solution.

Categories