Add class to PHP if SQL field is 'Yes' - php

I'm creating a site that lists custom userbars in a forum, and am using both PHP and SQL to achieve this easily.
My question is:
If a field is listed 'active' for the group, how do I go about adding a class specifically for that, to enable a glow around the Bootstrap card?
Here's my current code (forgive me if it's terrible)
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
</div>

You can use a ternary operator like this:
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql);
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card <?php $record['isActiveGroup'] === 'yes' ? 'ADD-CLASS-HERE' : ''; ?>">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
</div>
You can change the value of 'yes' to the value of the active group that is saved on your database.

Related

How do I filter with dropdown list? PHP and SQL

We are trying to filter based on the tags or the dropdown menu, we are trying to create a blog website that has tags and those tags can be used to filter the posted content on the homepage Tags dropdown menu:. Here should be the content in the page: Here should be the content in the page:
<nav>
<ul>
<li>Home</li>
<li>Tags<i class="material-icons">arrow_drop_down</i>
<ul class = "dropdown">
<li>All</button></li>
<li>Homemade</button></li>
<li>Pro</button></li>
<li>Resto</button></li>
</ul>
</li>
<!-- Posting -->
<div>
<?php
$query = "select * from posts order by date limit 8";
$result = mysqli_query($con,$query,$tagHomemade);
?>
<?php if(mysqli_num_rows($result) > 0):?>
<div>
<?php while ($row = mysqli_fetch_assoc($result)):?>
<?php
$user_id = $row['user_id'];
$query = "select username, image from users where id ='$user_id' limit 1";
$res = mysqli_query($con,$query);
$user_row = mysqli_fetch_assoc($res);
?>
<div class="card">
<div style ="display: flex;">
<div style ="flex:1", >
<div class="profile"><img class ="icon" src="<?=$user_row['image']?>"></div>
</div>
<div style ="flex:2;" >
<h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
<?php echo date("jS M, Y",strtotime($row['date']))?>
</h5>
<h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
</div>
</div>
<div>
<?php if (file_exists($row['image']))?>
<div class="img" style="text-align:center;">
<img class="postPic" src="<?=$row['image']?>">
</div>
</div>
<div>
<?php if (!empty ($POST['post']));?>
<?php echo $row['post']?>
</div>
</div>
<?php endwhile;?>
</div>
</div>
<?php endif;?>`
</div>
Set tags in the url like this:
<li>Homemade</button></li>
Then you can get it by $_GET['tag'] in your code. Then put it on the SQL query.
Good to know: It's strongly recommended to use prepared statements and don't use $_GET['tag'] directly in your SQL code. It prevents SQL injection and cares about special characters as well.
Update:
<nav>
<ul>
<li>Home</li>
<li>Tags<i class="material-icons">arrow_drop_down</i>
<ul class = "dropdown">
<li>All</button></li>
<li>Homemade</button></li>
<li>Pro</button></li>
<li>Resto</button></li>
</ul>
</li>
<!-- Posting -->
<div>
<?php
$where = !empty($_GET['tag']) ? "where tag = '" . $_GET['tag'] . "'" : "";
$query = "select * from posts $where order by date limit 8";
$result = mysqli_query($con,$query);
?>
<?php if(mysqli_num_rows($result) > 0):?>
<div>
<?php while ($row = mysqli_fetch_assoc($result)):?>
<?php
$user_id = $row['user_id'];
$query = "select username, image from users where id ='$user_id' limit 1";
$res = mysqli_query($con,$query);
$user_row = mysqli_fetch_assoc($res);
?>
<div class="card">
<div style ="display: flex;">
<div style ="flex:1", >
<div class="profile"><img class ="icon" src="<?=$user_row['image']?>"></div>
</div>
<div style ="flex:2;" >
<h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
<?php echo date("jS M, Y",strtotime($row['date']))?>
</h5>
<h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
</div>
</div>
<div>
<?php if (file_exists($row['image']))?>
<div class="img" style="text-align:center;">
<img class="postPic" src="<?=$row['image']?>">
</div>
</div>
<div>
<?php if (!empty ($POST['post']));?>
<?php echo $row['post']?>
</div>
</div>
<?php endwhile;?>
</div>
</div>
<?php endif;?>
</div>

How to fetch comments from mysql database without destroying my html tags

I want to give a little style for my comment section, here is the code without any css, but i want to give it some style
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<p>";
echo $row['author'];
echo "<br>";
echo $row['message'];
echo "<br>";
echo $row['time'];
echo "</p>";
}
} else {
echo "there are no comments!";
}
?>
</div>
<button>More comments</button>
and down here is my html section of which i want to appear while handling my php comments where USER, COMMENT and TIME are are stored in my database, here is the html, how can i echo the above variables into the below html tags ?
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5>USER</h5>
</div>
<div class="media-body response-text-right">
<p>COMMENT</p>
<ul>
<li>TIME</li>
<li>Reply</li>
</ul>
</div>
</div>
You can do like this:
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
</div>
hope it will help you.

fetching data from php Admin for display

I have a total 4 book of abc. I showing that author 1 book in book detail page. As well as i want to display remaining 3 books of author in below other books by this writer section. But in this section showing 1 book of that author. Please help me what can i do?
Book detail code:
<?php
if(isset($_GET['pid']) && !empty($_GET['pid'])){
$id = $_GET['pid'];
$result = $conn->query("SELECT * FROM bookrecord WHERE id ='$id' and status='a'");
if($result->num_rows > 0){
$imgData = $result->fetch_assoc();
extract($imgData);
?>
<div class="preview-pic tab-content">
<div class="tab-pane active" id="pic-1">
<img src="admin_pannel/book_images/<?php echo $imgData['file']; ?>" class="img-responsive" alt=""/>
</div>
</div>
<?php
}
}
?>
OTHER BOOKS BY THIS WRITER code:
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
$row = $result->fetch_assoc();
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
?>
This is my (book-detail.php) page and i come from this link:
<a class="btnpoem" href="book-detail.php?pid=<?php echo htmlentities($row['id']);?>&auth_name=<?php echo htmlentities($row['author_name']);?>">Download</a>
You need to apply while() in your second code:-
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
}
?>
Note:- Your queries are Wide-Open for SQL-INJECTION.
So try to use prepared statements.
Reference:-
PHP: mysqli::prepare - Manual
PHP: PDO::prepare - Manual
Just change your code from this code and in this code you have to change your sql query a lil bit to get rows except first one because you have got first row(Image), and instead of while loop you can also use foreach
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'" and WHERE ID NOT IN (SELECT MIN(ID) FROM your_table GROUP BY USER_ID));
if($result->num_rows > 0){
foreach ($result as $key){
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $key['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $key['book_title']; ?></h4>
<h5 class="text-center"><?php echo $key['author_name']; ?></h5>
</div>
<?php
}
}
}
?>`

Dynamic Html pages from Mysql with PHP

I'm working on a website with php and mysql and have some problems to generate web pages URL from database rows.
I have only 3 page connection.php (mysql connection) index.php (where show al products/contents thumbnails with button with product details URL) and details.php where i want show info for single product.
from index.php i add a link to redirect to details.php page with this:
<a href="details.php?id=<?php echo $row['ID']; ?>"
it's work but Big problem is in details.php because the script don't show a single products details, but show all products, please someone can help me? Thank you
index.php code
......other html code......
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 col-md-3">
<div class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
<div class="caption">
<h4><?php echo substr($row['Title'], 0, 30); ?></h4>
<p><?php echo $row['Brand']; ?></p>
<?php echo $row['ID']; ?>
<p>Cofronta Dettagli</p>
</div>
</div>
</div>
<?php
}
?>
......other html code......
connection.php code
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBname = "prodotti";
try {
$DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname",$DBuser,$DBpass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
die($ex->getMessage());
}
?>
details.php code
......other html code......
<div class="container">
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 stylerow">
<a href="<?php echo $row['AffiliateLink']; ?>" class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
</a>
</div>
<div class="col-sm-8 stylerow">
<h2><?php echo $row['Title']; ?></h2>
<p><?php echo $row['Brand']; ?></p>
<button type="button" class="btn btn-primary btn-lg">Amazon</button>
</div>
</div>
</div><!-- /.container -->
......other html code......
Add
$id=$_GET['id'];
edit following line in your code
$query = "SELECT * FROM campi_name";
To
$query = "SELECT * FROM campi_name where id="'.$id.'" ";

mysql query ironing one search result on webpage?

My query printed on webpage
SELECT * FROM products WHERE (product_name like '%meat%' OR description like '%meat%' OR ingradients like '%meat%') AND hide!=1 ORDER BY id ASC
If I run the same query query in mysql its showing 2 results with my php loop code its showing only one result,
my php code
<?php
$keyword=mysql_real_escape_string($_GET['Keyword']);
$query2 = "SELECT * FROM products WHERE (product_name like '%$keyword%' OR description like '%$keyword%' OR ingradients like '%$keyword%') AND hide!=1 ORDER BY id ASC ";
echo $query2 ;
$result2 = mysql_query($query2) or die('Error, query failed2');
if (mysql_num_rows($result2)>0){
mysql_data_seek($result2, 0);
$row2 = mysql_fetch_array($result2, MYSQL_ASSOC)
?>
<ul id="product-listing">
<?php
$i=1;
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)){ ?>
<li <?php $i; if ($i%3==0) {echo "class=\"last\"";} ?>>
<div class="img">
<?php if ( $row2['new'] ==1 ) { ?>
<div class="new"><img src="images/new.png" width="18" height="41" /></div>
<?php } ?>
<img src="images/products/284X190/<?php echo $row2['image_1']; ?>" width="284" height="190" alt="" title="" /> </div>
<div class="name"><?php echo $row2['product_name']; ?></div>
<form action="" method="post">
<div class="price">Price:
<?php if ( $row2['market_price'] !=0 ) { ?>
<span> $<?php echo $row2['market_price']; ?> </span>
<?php } ?>
$<?php echo $row2['price']; ?></div>
<div class="add-to-cart">
<input type="image" src="images/btn-1.jpg" />
</div>
<div class="clear"></div>
</form>
</li>
<?php $i++; } ?>
</ul>
<?php } else { ?>
No Products,
<?php } ?>
You should use a while loop in your php.
while($item = mysql_fetch_assoc($query))
{
print_r($item); // echo out whatever you need to for each item returned
}

Categories