Foreach loop not printing first form - php

I am updating the code of my search page, for some reason if you search any users for example there are three users with the name "Peter" and if you search for them the first Peter prints only the inputs:
<input type="hidden" name="chatting_with" value="userexample"/><input type="hidden" name="chatting_logusr" value="userexample"/><input id="prponclick" class="openchatmsgdevices" type="submit" value="Enviar un mensaje" />
But the rest print correctly with the form:
<form target="u-n" method="POST" action="open_new_chat_with_user"><input type="hidden" name="chatting_with" value="userexample"/><input type="hidden" name="chatting_logusr" value="userexample"/><input id="prponclick" class="openchatmsgdevices" type="submit" value="Enviar un mensaje" /></form>
Heres the full code:
<?php
session_start();
include("../DD/ddd.php");
$logged_user = $_SESSION['valid_user'];
if(!$logged_user){return false;}
$query = "SELECT searched_for FROM searches WHERE `user` = '$logged_user' ORDER BY id_search DESC LIMIT 1";
$run_query = $all_conn->query($query);
$rows_searches = $all_conn->query($query)->fetch();
$searched_for = $rows_searches['searched_for'];
$no_allowed_spaces = preg_replace('/\s/', '', $searched_for);
$query_info = "SELECT * FROM user_info WHERE nombre LIKE '%".$searched_for."%' OR `user` LIKE '%".$searched_for."%'";
echo '<div class="n-ovf" style="overflow:auto;">';
if(strlen($searched_for) >= 1)
{echo '<div id="search-ico-con"><div id="s-ico-con"></div><div id="s-l-dvsr-con"><div id="s-l-dvsr"></div></div><div id="s-txt-con"><h3>'.$searched_for.'</h3></div></div><div id="s-dvsor"></div>'.
'<div id="search-hastag-con"><div id="s-hastag-con"><h1>#</h1></div><div id="shtag-l-dvsr-con"><div id="shtag-l-dvsr"></div></div><div id="shtag-txt-con"><h3>'.$no_allowed_spaces.'</h3></div></div><div id="shtag-dvsor"></div>';}
foreach($all_conn->query($query_info) as $got_users){
if(strlen($searched_for) == 0){echo '<div class="search-for-something"><h3>Buscar algo....</h3></div>'; return false;}
echo '<div class="searched-content-info">
<div style="float:left; width:100%; height:0.1px; position:relative; z-index:9586;" class="container-buttons-search-user">
<form target="u-n" method="POST" action="open_new_chat_with_user"><input type="hidden" name="chatting_with" value="'.$got_users['user'].'"/><input type="hidden" name="chatting_logusr"
value="'.$logged_user.'"/><input id="prponclick" class="openchatmsgdevices" type="submit" value="Enviar un mensaje" />
</form>
<input type="button" value="Visitar árbol"/>
</div>'.
'<div class="searched-photo"><img src="'.$got_users['foto'].'"></div>
<div class="searched-names"><h3>'.$got_users['nombre'].'</h3></div>
</div>
<div class="divisor-search-user"></div>';
}
echo '<div class="all-searched-content-divisor"></div>';
echo '</div>';
?>

Related

How i can get value from radio button form in php?

Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="post">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Its html page to ask question
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label for='.$question_body.'>Yes</label>
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
Basically this form asked question whether yes or no using radio button. $option_a == 'Yes' and $option_b == 'No'. The question is like this "Are you have fever ?". So when i submit the value did not pass to the 'Answer.php' page.
'Answer.php' page.
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
In this page have error undefined_index value but still echo succeed.
Your HTML code should look like the following:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
You can use PHP to insert whatever values you want but you need the radio button name to be the same.
Then if you want to echo that in PHP you'd use:
echo $_POST['whatevername']; //same name you used in the form
You are passing value as name for radio buttons
name="'.$option_a.'"
This will result you in name ="Yes"
And you are trying to fetch echo $_POST['option_a']; where option_a is not defined.
Try this
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
Same for other radio button
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>

How to loop or foreach it

I cannot foreach loop my comment & reply system. I read many article and example but can't apply these in my case.
This is my comment system with reply, here every think is working well. But reply form work for only last/new comment. So i need to loop each comment. But can't do, please help me.
global $dbh;
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='$tutid' ORDER BY id DESC LIMIT 5") or die(mysqli_error($dbh));
echo'<div class="comments"><div id="updates"><div class="content"><comment>';
// Show only main comment
while($row = mysqli_fetch_array($results))
{ $id = $row['id'];
$qazi_id = $row['qazi_id'];
$username = $row['username'];
$description = $row['description'];
$parent_id = $row['parent_id'];
$date = $row['date'];
//echo comment
echo'<div class="comment">
<div class="cdomment_cheder">';
echo'<p>'.$username.' Says:</p>';
echo'<span>'.$date.'</span><br/>
<div class="avatarcnt">
<img alt="" src="uploadprofile/'.$u_imgurl.'"/>
</div></div></div>
<div class="cdomment_text">';
if ($description=="") {echo '';}
else echo''.htmlentities($description).'<br>';
echo '</div>';
//reply
echo'<div class="reply_box">Reply</div>';
//Reply form
echo'<div id="loader"></div><div class="reply_here" id="reply_here-'.$id.'" style="display:none;">
<form action="" method="post" id="repfrm" enctype="multipart/form-data">
<fieldset id="cmntfs">
<input type="hidden" name="username" tabindex="1" id="author" value="'.$username.'"/>
<textarea name="replycom" rows="2" tabindex="4" id="replycom" value=""></textarea>
<input type="hidden" name="parent_id" id="parent_id" value="0" />
<input type="hidden" name="tutid2" id="tutid" value="'.$tutid2.'" />
<button type="submit" name="submit" value="" tabindex="5" id="submit" class="repfrm">Post Reply</button>
</fieldset>
</form>
</div>';
// Show Reply of each comment
$query = "SELECT * FROM comments_reply WHERE parent_id ='".$id."'";
$res = mysqli_query($dbh,$query);
while($row = mysqli_fetch_array($res))
{ $id = $row['id'];
$qazi_id = $row['qazi_id'];
$username = $row['username'];
$description = $row['description'];
$parent_id = $row['parent_id'];
$date = $row['date'];
//echo reply
echo' <div class="rcontent"><replycomment><ul>
<div class="comment">
<div class="cdomment_cheder">';
echo'<p class="name">'.$username.' Says:</p>';
echo'<span>'.$date.'</span><br/>
<div class="avatarcnt">
<img alt="" src="uploadprofile/'.$u_imgurl.'"/>
</div></div></div>
<div class="cdomment_text">';
if ($description=="") {echo '';}
else echo''.htmlentities($description).'<br>';
echo '</div>';
echo'</ul><replycomment></div>';
} //reply while close
} //comment while close
echo'</div><comment></div>';

show delete button on specific user who logged in

I am trying that the user who is logged in can delete his own posts so only he should see the delete button on his posts. I was thinking by my own that I had to bind the user who is logged in to the posts/img ID and then he should see the button. I'm struggling with this a bit so every useful comment is appreciated!
PHP
<?php
$result = $mysqli->query("SELECT users.user_id, users.username,
picas.img_id, picas.user_id, picas.name, picas.description, picas.created_at
FROM users
JOIN picas ON users.user_id = picas.user_id
ORDER BY picas.created_at DESC");
while($pica = $result->fetch_assoc()) {
$ses_user = $_SESSION['username'];
echo '<div class="image_post">';
if(isset($ses_user) == $pica['user_id'] && $pica['img_id']) {
echo '<form action="logic/delete_post.php?id='.$pica['img_id'].'" method="POST">
<input type="hidden" name="id" value="?id='.$pica['img_id'].'" />
<input type="submit" name="deleteSubmit" value="Delete" class="delete_post" />
</form>';
}
echo '<div class="user_avatar"><img src="avatars/'.$pica['username'].'.jpeg" /></div>
<div class="user_name">'.$pica['username'].'</div> <br><br><br><br>
<div class="timeago">'.$diff.'</div>
<div class="image_description">'.$pica['description'].'</div>
<img src="'.$pica['name'].'" />
<div class="clear"></div>
</div>';
}
?>
Your problem is that you are checking isset($ses_user) == $pica['user_id'] being isset() a function that returns a bool.
What you want to check is if the current user is the owner.
The correct condition should be:
...
if(isset($ses_user) && ($ses_user == $pica['username']) && $pica['img_id']) {
echo '<form action="logic/delete_post.php?id='.$pica['img_id'].'" method="POST">
<input type="hidden" name="id" value="?id='.$pica['img_id'].'" />
<input type="submit" name="deleteSubmit" value="Delete" class="delete_post" />
</form>';
}
...

How to get the ID when selected the particular item?

I have a page that display products and I would like to get the product ID when I click on the particular item and pass it to another page.
May I know how can I achieve this?
I always get the last PID, my code:
<head>
<title>Toy-All</title>
<!--Wilmos: Using external CSS File to format the page style and fonts.-->
<link href="StyleSheet2.css" rel="Stylesheet" type="text/css" />
</head>
<body>
<form method = "post" action "getpid.php">
<div class="stylediv2-Middle-Toy-all">
<div class="transbox-Toy-all">
<?php
//open connection to MySQL Server
$connection = mysql_connect('localhost','root', '')
or die ('Unable to connect to !');
// select database for use
mysql_select_db('we-toys') or die ('Unable to select database!');
$query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
echo '<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'" width=200px height=200px; ?> </div>
<h3>'.$row[1].'</h3>
<h1><span style="color:red"> $ '.$row[7].' </span>
<input type="hidden" name="pid" value= '.$row[0].' >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</h1>
';
}
}
else
{
echo "No rows found!";
}
mysql_free_result($result);
mysql_close($connection);
?>
</div>
</div>
</form>
</body>
</html>
If you retrieve your data from $_SESSION['PID'], then you will always get the last ID because you keep reassign new value to that session.
You can just achieve this with a link to the another PHP page. For example:
<a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a>
A more completed code as requested
<?php
$query = 'SELECT p.*, price.priceN FROM product p, pricing price
WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
?>
<?php while ($row = mysql_fetch_array($result)) { ?>
<h3><?php echo $row[1]; ?></h3>
<a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a><br><br>
<?php } ?>
And for anotherPage.php code
<?php
echo "You are trying to add this product ID to cart: " . $_GET['id'];
?>
You can use this form that i also provide in this code.
$query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
$pid = ($row[0]);
$_SESSION['PID'] = $pid;
echo '<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'" width=200px height=200px; ?> </div>
<h3>'.$row[1].'</h3>
<h1><span style="color:red"> $ '.$row[7].' </span>
<form method="post" action="cart.php">
<input type="hidden" name="pid" value= '.$row[0].' >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</form>
$pid = '.$row[0].';
</h1>
';
}
}
Now you should make a new page such as cart.php
echo $_POST['pid'];
If I understand you correctly, the following should work:
<form method="post" action="anotherPage.php">
<input type="hidden" name="id" value="<?php echo "$row[0]"?>"/>
<input id="AddtoCart-Btn" type="Submit" value="<?php echo "$row[0]" ?>" />
</form>
So basically when you click the product button, the id will be accessible in anotherPage.php
EDIT:
I rewrote your code to improve readability:
<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src=<?php echo $row[5]; ?> width=200px height=200px; ?> </div>
<h3><?php echo $row[1]; ?></h3>
<h1>
<span style="color:red"> $ <?php echo $row[7] ?></span>
<form method="post" action="cart.php">
<input type="hidden" name="pid" value=<?php $row[0]; ?> >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</form>
<?php $pid = $row[0]; ?>
</h1>
Avoid echo-ing out large chunks of HTML where possible. Try it now, If it fails provide the error message.
The above does, what the simple test below achieves:
<?php
$id = 1;
if (isset($_POST['submit_btn'])){
echo $_POST['id'];
}
?>
<form method="post" action="#">
<input type="hidden" name="id" value= <?php echo $id; ?> >
<input type="submit" name="submit_btn" value="submit">
</form>

PHP Multiple CheckBox Delete

I am having a very hard time solving my problem with the multiple checkbox delete. Can someone direct me to the solution? What is supposed to happen here is that the user can tick the boxes and click a delete button to delete the ticked ones. Unfortunately, my code doesn't seem to work; can you point me in the right direction?
<div id="container" class="page">
<img id="disclaimer" class="page" src="images/DISCLAIMER.png" alt="" />
<img id="logo" class="page" src="images/MI-LOGO.png" alt="" />
<div id="videoContainer" class="page">
<video id="video" controls>
<source src="video/animationTest.m4v" />
</video>
</div>
<div id="etc" class="page">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<textarea name="textPost" id="textPost">
</textarea>
<input type="submit" name="btnPost" id="btnPost" value="Post" />
<div id="displayOpacity">
<input type="text" name="display" id="display" value="0" />
</div>
<input type="submit" value="Delete" name="btnDelete" id="btnDelete" />
</div>
<div id="postItDiv" class="page">
<?php
$cxn = mysqli_connect('localhost','root','root','TimePost') or die(mysqli_error());
$selectQuery = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
$selectResult = mysqli_query($cxn,$selectQuery) or die(mysqli_error());
while($row = mysqli_fetch_assoc($selectResult))
{
$postz = $row['Post'];
$timez = $row['PostTime'];
$idNoz = $row['PostID'];
echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNoz.'<br />'.$postz.' at '.$timez.' seconds mark</p><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNoz.'"</input></div>';
}
if ($_POST['btnPost'] == "Post") {
$toPost = $_POST['textPost'];
$date = date("y-m-d");
$postTime = $_POST['display'];
$postTime = floor($postTime);
$insertQuery = "INSERT INTO tblTimePosts VALUES ('','Mimagazine Asia','Chelsea','$postTime','$toPost','$date')";
$insertResult = mysqli_query($cxn,$insertQuery) or die(mysqli_error());
$query = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
$result = mysqli_query($cxn,$query) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result))
{
$post = $row['Post'];
$time = $row['PostTime'];
$idNo = $row['PostID'];
echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNo.'<br />'.$post.' at '.$time.' seconds mark</p><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo.'"</input></div>';
}
}
else if($_POST['btnDelete'] == "Delete")
{
$tbl_name = 'tblTimePosts';
foreach ($_POST['checkbox'] as $id => $value)
{
$sql = 'DELETE FROM `'.$tbl_name.'` WHERE `PostID`='.(int)$id;
$resulta = mysqli_query($cxn,$sql);
if ($resulta > 0) {
echo "success";
}
else
echo "fail";
}
header('Location: videoJudge.php');
}
?>
</form>
</div>
<span id="copyright" class="page">Copyright © 2011<span style="color:#00AEEF;">mi</span>magazine.asia</span>
<span id="comment" class="page" style="color:#00AEEF;">comment/s</span>
<span id="download" class="page">(Right-click to download video)</span>
</div>
This HTML:
echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNoz.'<br />'.$postz.' at '.$timez.' seconds mark</p><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNoz.'"</input></div>';
should be:
echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNoz.'<br />'.$postz.' at '.$timez.' seconds mark</p><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNoz.'" /></div>';
This query on else if($_POST['btnDelete'] == "Delete") should be:
$sql = 'DELETE FROM `'.$tbl_name.'` WHERE `PostID`='.(int)$value;

Categories