How insert a new line for each result of query select - php

I checked all the similar questions but I didn't find an answer.
I wanna insert a <br/> after each result of a query select but i can't do it, maybe because I use float property in CSS.
I'll explain my problem with code:
$sqlUser = "SELECT id,username FROM utenti where id IN (select idUtente from motopartecipanti where idItinerario = '".$idViaggio."') ORDER BY username";
$resultUser = $mysqli->query($sqlUser);
while($rowUser = $resultUser->fetch_assoc()) {
$username = $rowUser["username"];
$idUtente = $rowUser["id"];
echo '
<div style="float:left;">
<label>'.$username.'</label>
</div>
';
$sqlTipo = "SELECT tipo FROM moto where id IN (select idMoto from
motopartecipanti where idItinerario = '".$idViaggio."' AND idUtente =
'".$idUtente."') ";
$resultTipo = $mysqli->query($sqlTipo);
while($rowTipo = $resultTipo->fetch_assoc()) {
$tipo = $rowTipo["tipo"];
echo '
<div style="float:right;">
<label>'.$tipo.'</label>
</div>
';
}
}
The code works but the output is:
Username1Username2 Type1Type2
Instead it should be:
Username1 Type1
Username2 Type2
I used mysql tag too because maybe it can be a problem of query syntax

Add a '' tag at the end of $tipo 'div' tag
while($rowTipo = $resultTipo->fetch_assoc()) {
$tipo = $rowTipo["tipo"];
echo '
<div style="float:right;">
<label>'.$tipo.'</label>
</div><div style="clear:both">
';
}

You can echo <br/> tag or I think its better if you echo a row rather than `. However this depends on your requirement.
echo '
<div style="float:right;">
<label>'.$tipo.'</label> <br/>
</div>
';

Related

mysqli_num_rows generates unlimited amount of data

so, I'll try to explain it as good as I can with my knowledge in english.
I am trying to count data from my database, basically - how many fields I have with the same ID. here's image from database.
image from database
for example, in posts i have review system and it works, i have this PHP code to count how many same fields i have with review and id.
Here's the code:
<?php
$revposid = $res['content_id'];
$pos="SELECT review FROM comments WHERE review='positive' and postid=$revposid";
$neg="SELECT review FROM comments WHERE review='negative' and postid=$revposid";
$neu="SELECT review FROM comments WHERE review='neutral' and postid=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neg)){$rowcountneg=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neu)){$rowcountneu=mysqli_num_rows($result);}
?>
<div class="reviews" id="reviews">
<span class="good"><b class="fa fa-thumbs-up"></b><?php echo $rowcountpos ?></span>
<span class="neutral"><b class="icon-thumbs-up"></b><?php echo $rowcountneu ?></span>
<span class="bad"><b class="fa fa-thumbs-down"></b><?php echo $rowcountneg ?></span>
</div>
and when I try to use the same code
$revposid = $cont['content_id'];
$pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
in my other script I have like system, it should show all my likes and under likes total likes of the post but when I use it it shows unlimited amount of data, i have no idea why. Here's the full code, I would appreciate some help or explanation.
<?php $ususername = $_GET['user_username'];$sql = "SELECT * FROM user_details WHERE user_username='$ususername'";$usresult = mysqli_query($_db,$sql);?>
<?php if( ! mysqli_num_rows($usresult) ) {
echo " Ooops? <br> <br>User <b>".$_GET["user_username"]."</b> doesn't exist.";
} else {
while($usrow = mysqli_fetch_array($usresult,MYSQLI_BOTH)) {?>
<?php
$current_user = $usrow['user_id'];
if ($_db->connect_error) {
die("Connection failed: " . $_db->connect_error);
}
$sql = "SELECT * FROM user_content_like WHERE user_id=$current_user ORDER BY date_added DESC;";
$result = $_db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$content_id = $row['content_id'];
$sql1 = "SELECT * FROM content WHERE content_id=$content_id";
$result1 = $_db->query($sql1);
if ($_SESSION['user_id'] == $usrow['user_id']) {$output = '
<button type="button" class="unlike_button" onclick="unlike(this);" name="like_button" data-content_id="'.$row["content_id"].'" ><i class="fa fa-minus"></i></button>
';} else {
$output = '';
}
while($cont = $result1->fetch_assoc()) {
$revposid = $cont['content_id'];
$pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
echo '
<div class="community-feed-thread">
<div class="community-icon-thread"></div>
<div class="community-comments-thread">'.$output.'</div>
<a href="'.$cont["content_id"].'" class="community-title-thread"><h3>'.$cont["title"].'</h3>
<span class="likes-desc"> Total likes: '.$rowcountpos.'</span>
</a>
</div>
';
}
}}
else {
echo " hmmmmmmmmmmmm.<Br><br>". $usrow["user_username"]." doesn't like anything. ";
}
$_db->close();
?>
<?php }}?>
this is how i want it to look
This is how it looks
Your re-using the same variable for different result sets in the code...
$result = $_db->query($sql);
and
if ($result=mysqli_query($_db,$pos))
You will need to ensure you only use the variable name once or it may have side effects in other loops.

Embedded html within while-loop to display result based on what data is returned

Hi Friends
The code I've presented displays data (moduleID & moduleName) associated with a log in user. Presently it shows specific html code associated with a moduleID that should be displayed on the screen when the user logs in. I want to implement a way to iterate through the values (Possibly if-else statement within loop) in order the specific html code i have presented to be shown when a user has one or many moduleID's attached to them.
Here is my homepageDemo.php
<?php
session_start();
//including the database connection file
include 'config.php';
$cuserID= $_SESSION['userID'];
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT m.moduleID, m.moduleName
FROM modules m
JOIN courses c ON m.courseID = c.courseID
JOIN usersDemo u ON c.courseID = u.courseID
WHERE userID = '$cuserID'"); // using mysqli_query instead
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='IS4439Function()'>IS4437</a></p>
</div>
";
}
?>
Here is the html code attached to the moduleID of IS4437
<div id="loader-wrapper">
<div id="loader"><div id="loader1">
</div>
</div>
<p><a onclick="IS4439Function()">IS4437</a></p>
</div>
Here is the html code attached to the moduleID of IS4408
<div id="loader-wrapper1">
<div id="loader"><div id="loader1">
</div>
</div>
<p><a onclick="IS4408Function()">IS4408</a></p>
</div>
EDIT
So as I've mentioned the problem i'm currently having, these are the different methods I've already tried.
Originally before embedding the html into the while loop in the homepageDemo.PHP, I simply returned the moduleID & moduleName associated with logged in user as text
<?php
session_start();
//including the database connection file
include 'config.php';
$cuserID= $_SESSION['userID'];
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT m.moduleID, m.moduleName
FROM modules m
JOIN courses c ON m.courseID = c.courseID
JOIN usersDemo u ON c.courseID = u.courseID
WHERE userID = '$cuserID'"); // using mysqli_query instead
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>"."<a>".$res['moduleID']."</a>"."</td>";
echo "<td>"."<a>".$res['moduleName']."</a>"."</td>";
}
?>
Then I attempted to iterate through the values to return the html code associated with each individual moduleID
SWITCH STATEMENT ATTEMPT:
while($res = mysqli_fetch_array($result)) {
switch ($cuserID) {
case 1:
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='IS4439Function()'>IS4437</a></p>
</div>
";
break;
case 2:
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='IS4408Function()'>IS4408</a></p>
</div>
";
break;
case 7:
echo"
<div id='loader-wrapper'>
Functions called
function IS4439Function() {
{
window.location.href = "global.php";
}
}
function IS4408Function() {
{
window.location.href = "isStrategy.php";
}
}
function IS4449Function() {
{
window.location.href = "webApp.php";
}
}
Unfortunately neither were what I was trying to achieve. Any thoughts?
You're really close, and I'm not exactly sure where the disconnect is, but here you go. You need to replace the hardcoded moduleID value (IS4439) with the value that you've already pull from the db. Putting together what you already have, it really just comes down to concatenation.
while($res = mysqli_fetch_array($result)) {
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='" . $res['moduleID'] . "Function()'>". $res['moduleID'] . "</a></p>
</div>
";
}
I would suggest though, that instead of calling different functions, you consider calling just one function, and passing in the moduleID as a parameter and then act accordingly within the function.
Based on your update, you would then have this...
while($res = mysqli_fetch_array($result)) {
?>
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='redirectFunction("<?php echo $res['moduleID']; ?>");'><?php echo $res['moduleID']; ?></a></p>
</div>
<?php
}
?>
and your function would be...
function redirectFunction(moduleID)
{
var newLocation = "";
if(moduleID == "IS4439")
{
newLocation = "global.php";
}
else if(moduleID == "IS4408")
{
newLocation = "isStrategy.php";
}
else if(moduleID == "IS4449")
{
newLocation = "webApp.php";
}
window.location.href = newLocation;
}

Comment reply select not working good

I have a problem and I don't know how to resolve...
I have page with comments and page with replies.
Down is my code, everything is ok, it shows all I want but the problem is, when I click on the comment to go to the page with a reply, the id it gets is wrong.
Example: for the first comment I need to have id=1, second id=2 etc. But I get id=1 for all the comments where user_id = 1 and not id from comment.
Sorry, my English.
<?php
$id = $show['id'];
$sq = "SELECT * FROM comments, users WHERE comments.user_id = users.id";
$re = mysqli_query($dbCon, $sq);
while($abc=mysqli_fetch_assoc($re)){
?>
<div class="card hoverable q_area">
<div class="card-content">
<div class="chip">
<img src="<?php echo $show['profile_foto'] ?>">
<?php echo $abc['user'] . " said:"; ?>
</div><br />
<div id="comm">
<?php $a=substr(str_replace(' ','-',$abc['question']), 0, 50); ?>
<h5><a href="replys.php?id=<?php echo $abc['id'] ?>&reply=<?php echo $a ?>">
<?php echo $abc['comment']; ?></a></h5> <br />
</div>
</div>
</div>
<?php } ?>
If I change that select like this:
$sq = "SELECT * FROM comments";
then link id work, but I can't echo the user who said the comment.
database tables:
users - id, user, pass
comments- id, user_id, comment
please help me :(
Change Your query - $sq to this one:
$sq = "SELECT a.*, b.user FROM comments as a, users as b WHERE a.user_id = b.id"
You just nedd a alias in your select;
$sq = "SELECT *, comments.id as comment_id FROM comments, users WHERE comments.user_id = users.id";
And your link will be some like this:
<h5><a href="replys.php?id=<?php echo $abc['comment_id'] ?>&reply=<?php echo $a ?>">
I hope I've helped.

PHP while loop different images

Okay let me clarify everything.
Chars table looks like http://prntscr.com/9v8jiv .
Here is the standard html
<div id="characterone">
<h1 class="charactername">Test_Name</h1>
<p class="characterstats">DOB: 12/01/1992</p>
<p class="characterstats">Origin: Mexican</p>
<p class="characterstats">Time in Los Santos: 56</p>
<button class="choosebutton">Choose</button>
</div>
<div id="charactertwo">
<h1 class="charactername">Angelo_Damce</h1>
<p class="characterstats">DOB: 12/01/1992</p>
<p class="characterstats">Origin: American</p>
<p class="characterstats">Time in Los Santos: 26</p>
<button class="choosebutton">Choose</button>
</div>
<div id="characterthree">
<h1 class="charactername">Moemen_Walid</h1>
<p class="characterstats">DOB: 12/01/1992</p>
<p class="characterstats">Origin: American</p>
<p class="characterstats">Time in Los Santos: 26</p>
<button class="choosebutton">Choose</button>
</div>
All I want to do is, to select someone's logged in characters and then he sees the 3 in front of him and then he chooses,
I began by normaling querying them
$getchars = mysqli_query($con,"SELECT * FROM `characters` where Username = '".$user."'") or die('Error: ' . mysqli_error($con));
$check = mysqli_num_rows($getchars);
Now I need to find a way so for every char the mysql query find, to be echo'ed or written in the html up.
Eg: echo ''.$row[1]["Name"].' ';
but in each div there should be a different echo
'.$row[2]["Name"].'
^ But of course this code isn't real. just trying to explain.
Ok Here is the complete answer for your question. Just replace the Name and Time Column names with the correct Column names in the database.
$i = 1;
while($rowget = mysqli_fetch_array($getchars))
{
$id1 = 'characterone';
$id2 = 'charactertwo';
$id3 = 'characterthree';
if($i<4){
echo'<div id="'.${'id'.$i}.'">';
echo '<h1 class="charactername">'.$rowchar["Name"].'</h1>
<p class="characterstats">DOB: '.$rowchar["Birthdate"].'</p>
<p class="characterstats">Origin: '.$rowchar["Origin"].'</p>
<p class="characterstats">Time in Los Santos: '.$rowchar["Skin"].'</p>
<button class="choosebutton">Choose</button>';
echo '</div>';
}
$i++;
}
just use a counter
$i=1;
while($rowget = mysqli_fetch_array($getchars)) {
$charpost = $rowget["Character"];
echo' <img src=$i.png> ';
$i++;
}

why is it it that the record appear even if the the record was not on the category

Hi guys can you help me with my problem.
when I search other categories it appears the record on my other category what happen. The query shows that it will only search the record if the category is correct.
For example record: catname: IT record from my IT is web my other catname is History record is History123 if i search History as my category and blank on search it will appear all
Here is my code:
I hope you can help me im a newbie in php
<form method="post">
<input type="text" name="search" class="searchfield" placeholder="Search for Book Title, Author, ISBN COde or Location">
<select name="catname" class="searchfield">
<option>--------Categories--------</option>
<?php
$query = "select * from categories";
$result = $con->query($query) or trigger_error('Wrong SQL: ' . $query . ' Error: ' . $conn->error, E_USER_ERROR);
$result ->data_seek(0);
while ($row =$result->fetch_assoc())
{
# code...
echo'<option>'.$row['catname'].'</option>';
}
?>
</select>
<input type="submit" name="searchmo" value="Search" class="btn btn-info btn-lg">
<h2 class="sub-header">List of Books</h2>
<div class="table-responsive">
<!--author LIKE '%$search%' or bookcode LIKE '%$search%' or location LIKE '%$search%'
or -->
<?php
if(isset($_REQUEST['searchmo']))
{
$catname = $con->real_escape_string($_POST['catname']);
$search = $con->real_escape_string($_POST['search']);
$query1 = $con->query("select bookname,bookid,bookdesc,location,
categories.catname,qty,bookcode,copyright,editionnumber,publisher,author
from categories inner join books on books.catname = categories.catname
where books.bookname LIKE '%$search%' or books.author LIKE '%$search%' or
books.bookcode LIKE '%$search%' or books.location = '%$search%' or
books.publisher LIKE '%$search%'
and categories.catname = '$catname'") or die(mysqli_errno());
if($query1->fetch_assoc() == 0)
{
echo '<div class="alert alert-warning">No result found.</div>';
}
else
{
$catname = $con->real_escape_string($_POST['catname']);
$search = $con->real_escape_string($_POST['search']);
$query = $con->query("select bookname,bookid,bookdesc,location,
categories.catname,qty,bookcode,copyright,editionnumber,publisher,author
from categories inner join books on books.catname = categories.catname
where books.bookname LIKE '%$search%' or books.author LIKE '%$search%' or
books.bookcode LIKE '%$search%' or books.location = '%$search%' or
books.publisher LIKE '%$search%'
and categories.catname = '$catname'") or die(mysqli_errno());
//$result ->data_seek(0);
while($row = $query->fetch_assoc())
{
?>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title"><strong>Book Title:<?php echo $row['bookname']?>- ISBN: <?php echo $row['bookcode']?> </strong></h3>
</div>
<div class="panel-body" style="line-height:10px;">
<p>Author: <?php echo $row['author']?></p>
<p>Publisher: <?php echo $row['publisher']?></p>
<p>Location: <?php echo $row['location']?></p>
<p>Quantity: <?php echo $row['qty']?> book's available</p>
<p>Summary:<?php echo $row['bookdesc']?></p>
</div>
</div>
<?php
}
}
}
?>
echo your query so you can see what it looks like after replacing the variables.
The way you currently have it LIKE '%$search%' is going to look like LIKE '%"searchterm"%'. Because you are adding quotes around the string ahead of time.
So assign to $search like so: $search = $con->real_escape_string($_POST['search']);
Side note: you should look in to prepared statements.

Categories