display multiple value of same column in mysqli php - php

I want to display value in checkbox. i have multiple value in database as shown in pic above. please have a look. I want value in different checkbox not in same checkbox
]1]2
Code I am trying
<div class="list-group">
<h3>Name</h3>
<?php
$query = "select distinct(name) from info_user where user_status = '1'";
$rs = mysqli_query($con,$query) or die("Error : ".mysqli_error());
while($color_data = mysqli_fetch_assoc($rs)){
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php echo $color_data['name']; ?>" >
<?php echo $color_data['name']; ?></a>
<?php } ?>
</div>
and what i tried by myself
<div class="list-group">
<h3>Name</h3>
<?php
$column = array();
$query = "select name from info_user where user_status = '1'";
$rs = mysqli_query($con,$query) or die("Error : ".mysqli_error());
while($color_data = mysqli_fetch_assoc($rs)){
$column[] = $color_data['name'];
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php foreach($column as $value)echo $value['name']; ?>" >
<?php foreach($column as $value)echo $value['name']; ?></a>
<?php } ?>
</div>
Getting this error after trying code

I think this is what you need.
<div class="list-group">
<h3>Name</h3>
<?php
$column = array();
$query = "select name from info_user where user_status = '1'";
$rs = mysqli_query($con,$query);
while ($color_data = mysqli_fetch_assoc($rs)) {
$column = array_merge($column, explode(',', $color_data['name']));
}
$column = array_unique($column);
foreach ($column as $value) {
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php echo $value; ?>" >
<?php echo $value; ?>
</a>
<?php } ?>
</div>

you can try this way, update your code
<div class="list-group">
<h3>Name</h3>
<?php
$column = array();
$query = "select distinct(name) from info_user where user_status = '1'";
$rs = mysqli_query($con, $query);
while ($color_data = mysqli_fetch_assoc($rs)) {
$column = array_merge($column, explode(',',$color_data['name']));
}
// to remove repeated names
$column = array_filter($column);
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php
foreach ($column as $value) {
echo $value;
?>">
<?php
echo $value;
}
?>
</a>
</div>

Related

Filter not filtering

I am trying to make a filter for website of cars. I would like to be able to filter by color of car represented by exterior. I do this by Select Distinct from exterior colors to list all of the exterior colors.
<h6 class="text-info">Select Color</h6>
<ul class="list-group">
<?php
$sql="SELECT DISTINCT exterior FROM newcars ORDER BY exterior";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" name="" value="<?= $row['exterior']; ?>" id="exterior"><?= $row['exterior']; ?>
</label>
</div>
</li>
<?php
}
?>
</ul>
<div id="test">
<?php
$sql = "SELECT * FROM newcars";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo ("<a href='newcarindex.php?id={$row['id']}'><div class='car'>");
echo '
<tr>
<td>
<img src="data:image\jpeg;base64,'.base64_encode($row['photo']).'"/>
</td>
</tr>
';
echo "<h2 class=car-name>";
echo $row['name'];
echo "</h2>";
echo "<span class=stock>STOCK#";
echo $row['stock'];
echo "</span>";
echo "<h3 class=car-msrp>";
echo $row['msrp'];
echo "</h3>";
echo "</div></a>";
}
} else {
echo "There are no Comments!";
}
?>
</div>
action.php this page i link to get the the results from what color they select. But I still cannot get it filter to the results.
include 'dbh.php';
if(isset($_POST['action'])) {
$sql = "SELECT * FROM newcars WHERE class !=''";
if(isset($_POST['class'])) {
$class = implode("','", $_POST['class']);
$sql .="AND class IN('".$class."')";
}
if(isset($_POST['body'])) {
$body = implode("','", $_POST['body']);
$sql .="AND class IN('".$body."')";
}
if(isset($_POST['exterior'])) {
$exterior = implode("','", $_POST['exterior']);
$sql .="AND class IN('".$exterior."')";
}
$result = $conn->query($sql);
$output='';
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$output .='<a href='newcarindex.php?id={$row['id']}'><div class='car'>
<tr>
<td>
<img src="data:image\jpeg;base64,'.base64_encode($row['photo']).'"/>
</td>
</tr>
<h2 class=car-name>'.$row['name'].'</h2>
<span class=stock>STOCK#'.$row['stock'].'</span>
<h3 class=car-msrp>'.$row['msrp'].'</h3>
</div></a>'
}
} else {
$output ="<h3>No Results</h3>";
}
echo $output;
}
?>
First of all you need to wrap your checkboxes in a HTML form that will send data to action.php file:
<h6 class="text-info">Select Color</h6>
<form action="action.php">
<ul class="list-group">
<?php
$sql = "SELECT DISTINCT exterior FROM newcars ORDER BY exterior";
$result = $conn->query($sql);
while($row=$result->fetch_assoc()) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" name="" value="<?= $row['exterior']; ?>" id="exterior"><?= $row['exterior']; ?>
</label>
</div>
</li>
<?php
}
?>
</ul>
<input type="submit" value="Filter" />
</form>
<div id="test">
...
Then, in action.php file you need to revise your SQL query. At this moment, when you concatenate base $sql variable with the one from if(isset($_POST['exterior'])) condition, the query looks like:
SELECT * FROM newcars WHERE class !=''AND class IN( ...
making it invalid (notice no space between !='' and AND).
Then the while loop has mixed apostrophes and quotation marks, which makes PHP code invalid. It should look like this:
while($row=$result->fetch_assoc()){
$photo = base64_encode($row['photo']);
$output .= "<a href='newcarindex.php?id={$row['id']}'>
<div class='car'>
<tr>
<td>
<img src='data:image\jpeg;base64,{$photo}'/>
</td>
</tr>
<h2 class='car-name'>{$row['name']}</h2>
<span class='stock'>STOCK#{$row['stock']}</span>
<h3 class='car-msrp'>{$row['msrp']}</h3>
</div></a>";
}

How do i insert these data from mysql into my webpage by php?

am trying to pull data from database of a number of associated rows into ROW1, ROW2 and ROW3 but using
<?php echo $row['']; ?>
but it's not working, please any idea
here is my code,
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
}
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
Well it looks like you are closing your while loop before you echo your results. Just move the closing bracket } after the closing </li>
Your code should look like this:
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
}
}
?>
Try this code
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $data) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $data['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $data['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
}
}
}
?>

php and mysql to create tabs with bootstrap 3

enter image description here
How can I make a loop with php where I separate the comments as rounds
All comments for the round 1
<div id="$row['round']">
$row['comments']
</div>
All comments for the round 2
<div id="$row['round']">
$row['comments']
</div>
but I can have many rounds, so I don't know the exact query to use
<div id="chat" class="chat">
<ul class="nav nav-tabs">
<?php
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<li><a data-toggle="tab" href="#menu'.$row['round'].'">'.$row['round'].'</a></li>';
}
?>
</ul>
<div class="tab-content">
<?php
$i=-1;
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<div class="tab-pane '.( $i = $i ? 'active' : '' ).'" id="menu'.$row['round'].'">';
$test = $row['round'];
$stmt2 = $DB->query("SELECT * FROM messages INNER JOIN system_users ON messages.user_id = u_userid WHERE $id = video_id AND $test = round ORDER BY date ASC");
while ($row2 = $stmt2->fetch()) { ?>
<br />
<p><b>round:</b> <?php echo $row2["round"]; ?></p>
<p><b>Message:</b> <?php echo $row2["message"]; ?></p>
<p><b>User:</b> <?php echo $row2["u_username"]; ?></p>
<p><b>time:</b> <?php echo date_format (new DateTime($row2["date"]), 'd|m|Y H:i:s'); ?></p>
<hr />
<?php
}$i =0;
echo '</div>';
}
?>
</div>
I am completely sure this is not the best solution, but it is working,
thanks anyway..

How can I get this to display in 1 loop

In the backend of my website, I have added more categories as follows
<strong>Tour category : </strong><br />
<select name="category_id">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 2: </strong><br />
<select name="category_id2">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id2']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 3: </strong><br />
<select name="category_id3">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id3']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
In the front end, I have this
<?php
$sql_ci = "SELECT * FROM location WHERE parent_id <> 0 ORDER BY parent_id, sort ";
$result_ci = mysql_query($sql_ci);
$cities = $Manager->fetchAssoc($result_ci); //peek_array($cities);
if(!empty($cities)){
foreach($cities as $city){
$i = 1; // for active category
$sql_count = "SELECT * FROM tour t, tour_category c
where t.location_id = {$city['id']} and t.type = 'P'
group by t.category_id AND t.category_id2"; //echo $sql_count.'<br />';
$result_count = mysql_query($sql_count);
$count = $Manager->fetchAssoc($result_count);
?>
<ul class="tours-accord tour-private" id="tourcity<?php echo $city['id'];?>">
<?php
$sql_ca = "SELECT * FROM tour_category ORDER BY sort";
$result_ca = mysql_query($sql_ca);
$categories = $Manager->fetchAssoc($result_ca);
if(!empty($categories)){
foreach($categories as $cat){
$sql_tour = "SELECT t.* FROM tour t
WHERE
t.touronline = 'yes'
AND t.category_id = {$cat['id']}
AND t.location_id = {$city['id']}
and t.type = 'P'
ORDER BY t.tourcode";
$result_tour = mysql_query($sql_tour);
$tours = $Manager->fetchAssoc($result_tour);
if(!empty($tours)){
?>
<li style="border-bottom:1px solid gray;">
<?php echo $city['name']; ?> - <?php echo $cat['name']; //echo ' '.$i.' ';?>
<ul>
<?php foreach($tours as $tour){
if(!empty($tour['intro_image'])){
$img = $tour['intro_image'];
}else{
$img = 'default.jpg';
}
?>
<a href="tour-details.php?code=<?php echo $tour['tourcode'];?>" class="tourname">
<li class="<?php if($i == count($count)){echo 'active';} ?>">
<img src="uploaded-items/tour-introimage/<?php echo $img; ?>" width="150" height="100" />
<span class="tourname"><?php echo $tour['tourname'].'(Code: '.$tour['tourcode'].')'; ?></span>
<p><?php echo $tour['intro']; ?></p>
<div class="home-tour-info">
<img src="images/btn-more-info-green-dark.jpg" alt="More info" class="moreinfo"/>
<div class="home-tour-price"><?php echo $tour['promotion_label']; ?></div>
</div>
</li></a>
<?php } ?>
</ul>
</li>
<?php $i++;}}} //each category ?>
The way I made it work was post the last bit 3 times and changed category_id to category_id2, category_id3. But this displayed duplicate categories.
How can I make all this just in 1 loop instead of 3?
Your backend loop is like wise as follows, as a optimising code,
<?
$numcount = 3;
for($i=0;$i<$numcount;$i++) { ?>
<strong>Tour category <?=$i+1?>: </strong><br />
<select name="category_id<?=$i+1?>">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id'.($i+1)]){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<? } ?>

Menu Items Not Closing?

I'm coding a staff panel but I'm stuck I've added a menu (navigation) but I'm stuck on how to go about opening it when clicked and closing it when clicked if open.
Here is the code i have so far;
<ul id="menu" class="nav">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="menustyle" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<div class="menutext"><?php echo $array['name']; ?></div>
</div>
<ul>
<li class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> onclick="Radi.menuToggle('<?php echo $array['id']; ?>');" style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' AND visible = '1' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<li>
<?php echo $array2['text']; ?>
</li>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</ul>
</li>
<?php
}
}
?>
</li>
</ul>
</div>
And this is the defualt code when you download radipanel;
<div style="float: left; width: 200px;">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="box">
<div class="square menu" style="background: #<?php echo $array['colour']; ?>;" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<img id="menutoggle_<?php echo $array['id']; ?>" class="menutoggle" src="_img/<?php echo ( $array['id'] != $array3['usergroup'] ) ? 'plus' : 'minus'; ?>_white.png" alt="Toggle" align="right" />
<strong><?php echo $array['name']; ?></strong>
</div>
<div class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<a href="<?php echo $array2['url']; ?>" class="<?php echo $i; ?>">
<?php echo $array2['text']; ?>
</a>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</div>
</div>
<?php
}
}
?>
</div>
So any ideas on why my code is not doing it?

Categories