Echoing html if data from SQL if statement true - php

So I've been struggling at how to echo this set of HTML with users that are set as admin from my database. I've looked at quite a few places for information but I'm struggling to get it to work. Perhaps I'm doing something really stupid. Thanks for your help.
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
?>

I think that you forgot to put the while between {}. Here is the correct code:
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
}
?>

Related

header hover problem any one have solution?

<?php
$q = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=? ORDER BY menu_order ASC");
$q->execute(array(0));
$res = $q->fetchAll();
foreach ($res as $row) {
$r = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=?");
$r->execute(array($row['menu_id']));
$total = $r->rowCount();
echo ' <li class="header_nav-list_item dropdown">';
if($row['page_id'] == 0) {
if($row['menu_url'] == '') {
$final_url = 'javascript:void(0);';
} else {
$final_url = $row['menu_url'];
}
?>
<a class="nav-link nav-item dropdown-toggle d-inline-flex align-items-center"
href="<?php echo $final_url; ?>"><?php echo $row['menu_name']; ?><?php if($total) {echo ' <i class="icon-angle-down icon"></i>';}; ?></a>
<?php
} else {
$r = $pdo->prepare("SELECT * FROM tbl_page WHERE page_id=? ");
$r->execute(array($row['page_id']));
$res1 = $r->fetchAll();
foreach ($res1 as $row1) {
?>
<a class="nav-link nav-item dropdown-toggle d-inline-flex align-items-center"
data-bs-toggle="collapse"
data-bs-target="#Menu"
data-trigger="dropdown"
aria-expanded="false"
aria-controls="Menu"
data-page="" href="<?php echo BASE_URL.URL_PAGE.$row1['page_slug']; ?>"><?php echo $row1['page_name']; ?>
<?php if($total) {echo '<em></em>';}; ?></a>
<?php
}
}
// Checking for sub-menu
$r = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=?");
$r->execute(array($row['menu_id']));
$total = $r->rowCount();
if($total) {
echo ' <div class="dropdown-menu collapse"><ul>';
$res1 = $r->fetchAll();
foreach ($res1 as $row1) {
//
echo '<li class="dropdown-list">';
if($row1['page_id'] == 0) {
if($row1['menu_url'] == '') {
$final_url1 = 'javascript:void(0);';
} else {
$final_url1 = $row1['menu_url'];
}
?>
<a class="dropdown-item nav-item" href="<?php echo $final_url1; ?>"><?php echo $row1['menu_name']; ?></a>
<?php
} else {
$s = $pdo->prepare("SELECT * FROM tbl_page WHERE page_id=?");
$s->execute(array($row1['page_id']));
$res2 = $s->fetchAll();
foreach ($res2 as $row2) {
?>
<a class="dropdown-item nav-item" href="<?php echo BASE_URL.URL_PAGE.$row2['page_slug']; ?>"><?php echo $row2['page_name']; ?></a>
<?php
}
}
echo '</li>';
//
}
echo '</ul></div>';
}
echo '</li>';
}
?>
This is my header code can any one tell me or updATE THIS CODE FOR ME
I WANT TO SOLVE MY PROBLEM

Adding categories & subcategories to a blog

Ok, I have done lots of research and looked at lots of stackoverflow questions but none have answered my question.
I'm building a simple blog and am at the moment i'm trying to build a simple categories/subcategories system but have hit an obstacle in making the subcategories load under their parent. If you know an easier method to the one i am doing below please let me know.
Here is how my db is structured:
id || name || parent_id || status
------------------------------------------
1 || category1 || NULL || 1
2 || subcategory1 || 1 || 1
Then i have my model code:
public function getCategories()
{
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = NULL');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
public function getSubCategories($parent)
{
$bind = [':parent' => $parent];
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = :parent');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
This is my controller code:
public function error()
{
$getSettings = $this->setting->getAll();
$getCategories = $this->setting->getCategories();
$getSubCategories = $this->setting->getSubCategories();
if(is_array($getCategories[0]) ) {
$isCategory = true;
} else {
$isCategory = false;
}
if(is_array($getSubCategories[0]) ) {
$isSubCategory = true;
} else {
$isSubCategory = false;
}
$data = [
'settings' => $getSettings,
'mainCategory' => $getCategories,
'subCategory' => $getSubCategories,
'isCategory' => $isCategory
'isSubCategory' => $isSubCategory
];
$this->view('index', $data);
}
Then I have my templatecode which is where im trying to do the foreach and the top category works fine but i just can't figure out how i do the subcategories. Before i started using the MVC model, i would simply just put the class inside the main category foreach and then put the id but now its all done in the controller so i cannot figure out how i do subcategories using the MVC model.
<?php foreach($data['mainCategory'] as $category) : ?>
<li class="dropdown">
<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#"><?php echo $category['name']; ?></a>
<div class="dropdown-menu" role="menu">
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 fas fa-box"></i> All <?php echo $category['name']; ?></a>
<div class="dropdown-divider" role="presentation"></div>
<?php foreach($data['subCategory'] as $scategory) : ?>
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 <?php echo $scategory['icon']; ?>"></i> <?php echo $scategory['name']; ?></a>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
Thanks very much!
$query = 'SELECT id, parent_id, name, icon FROM categories ORDER BY name';
$result = mysqli_query($conn, $query) OR trigger_error($query.'<br>'.mysqli_error($conn),E_USER_ERROR);
$refs = Array();
$categories = Array();
while($row = mysqli_fetch_assoc($result))
{
$thisref = &$refs[$row['id']];
$thisref['name'] = $row['name'];
$thisref['icon'] = $row['icon'];
if($row['parent_id']) $refs[$row['parent_id']]['children'][$row['id']] = &$thisref;
else $categories[$row['id']] = &$thisref;
}
echo '<ul class="nav navbar-nav mr-auto">';
foreach($categories as $category)
{
echo '<li class="dropdown">';
echo is_array($category['children'])
? '<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#">'.$category['name'].'</a>'.sub($category['children'])
: $category['name'];
echo '</li>';
}
echo '</ul>';
function sub(&$subCat)
{
echo '<div class="dropdown-menu" role="menu">
<a class="dropdown-item hide-me" role="presentation" href="#"><i class="fas fa-fire mr-2"></i> Most Popular </a>
<div class="dropdown-divider hide-me" role="presentation"></div>';
foreach($subCat as $id => $subcategory)
{
echo '<a class="dropdown-item" role="presentation" href="'.FULL_ROOT.'/category/'.$id.'/"><i class="'.$subcategory['icon'].' mr-2"></i>'.$subcategory['name'].'</a>';
}
echo '</div>';
}

smart PHP Pagination

I want to split pages to make it more smart and look nice,
I use this code to make Pagination
if (!isset($_GET['page'])){
$page = 1;
}
else {
$page = intval($_GET['page']);
}
$numbershownews = 1;
$x = ($page-1)*$numbershownews;
$select_news_all = $mysqli->query("SELECT id FROM news where FIND_IN_SET('$id_newscats_cat', cats) order by time desc");
$num_news_all = $select_news_all->num_rows;
$cn = $num_news_all/$numbershownews;
$select_news = $mysqli->query("SELECT * FROM news where FIND_IN_SET('$id_newscats_cat', cats) order by time desc limit $x,$numbershownews");
while ($rows_news = $select_news->fetch_array(MYSQL_ASSOC)){
$id_news = $rows_news ['id'];
$title_news = $rows_news ['title'];
}
i show pages by this code
<?
$pagenext = $page+1;
$pageprev = $page-1;
?>
<ul>
<li>
<?
if (($page == 1) or ($page == "")){
?>
<i class="fa fa-angle-double-right"></i>
<?
}else{
?>
<a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pageprev}.html"; ?>"><i class="fa fa-angle-double-right"></i></a>
<?
}
?>
</li>
<?php
for($i=0; $i<$cn; $i++){
$pagenumber = $i+1;
if($page == $pagenumber){
echo "<li>{$pagenumber}</li>";
}else{
?>
<li><a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pagenumber}.html"; ?>"><? echo $pagenumber; ?></a></li>
<?
}
}
?>
<li>
<?
if($page >= $cn){
?>
<i class="fa fa-angle-double-left"></i>
<?
}else{
?>
<a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pagenext}.html"; ?>"><i class="fa fa-angle-double-left"></i></a>
<?
}
?>
it works well but it gives me pages like this 1,2,3,4,5,6,7,8,9,10 i want to split pages like this 1,2,3,4,...,9,10 how can i do this
thanks

Nidified mysqli_fetch_assoc while

I have a problem with the following code:
$query = "SELECT * FROM movie_list WHERE id=$id";
$result_q = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result_q);
if ($row['movie_type'] == 'E' || $row['movie_type'] == 'S'){
$query_serie = "SELECT * FROM movie_list WHERE id_serie=$row[id_serie]";
$result_q_serie = mysqli_query($conn, $query_serie);
$query_serie_cont = "SELECT serie_number FROM movie_list WHERE id_serie = $row[id_serie] AND serie_number IS NOT null GROUP BY serie_number";
$result_q_serie_cont = mysqli_query($conn, $query_serie_cont);
[...]
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
}
?>
</li>
It works perfect the first while but the second time the while($row_serie) variable are missing. Debugging the page I see the $row_serie variable disappear after completed all the first while($row_serie_cont) but not reappearing at all when it was triggered the second time.
What I've miss in the code?
you close li after loop make it correct
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
echo "</li>"// inside the loop
}
?>
Seems that insert $result_q_serie = mysqli_query($conn, $query_serie);into the second while do the trick.
I don't understand why i need to repopulate result_q_serie is needed but now it works.
If someone can propose a better solution I'm hearing
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?>
</a>
<div class="collapsible-body">
<ul>
<?php
$result_q_serie = mysqli_query($conn, $query_serie);
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>
".$row_serie['episode_number']."</li>
"; } } echo "
</ul>
"; echo "
</div>
</li>
</ul>
</li>
"; } ?>
</ul>
seems that use mysqli_data_seek($result, 0); do the trick the right way.

display sub-menu of parent menu from parent id php

<?php
$page= $objPage-> get_page();
$id = isset($_GET['id']);
$child= $objPage-> get_child_page($id);
?>
<ul class="nav nav-tabs">
<li role="presentation" class="dropdown">
<?php
while($row = mysql_fetch_array($page) ) { ?>
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria- expanded="false">
<?php echo $row['title']; ?> <span class="caret"></span>
</a>
<?php } ?>
</li>
</ul>
Function get_page:
function get_page($id="") {
$sql = "SELECT "; if( !empty($field) ){
$sql .=" title FROM page WHERE page_id>1 " ;
} else {
$sql .= " * FROM page ";
}
if( !empty($id) ){
$sql .=" WHERE page_id=".$id;
} else {
$sql .=" WHERE parent_id= -1";
}
$result = mysql_query($sql) or die( mysql_error() );
if( !empty($id) && $result ){
$value = mysql_fetch_array($result) ;
return $value;
}
return $result;
}
Function get_child_page:
function get_child_page($id){
$sql = "SELECT * FROM page WHERE parent_id= $id";
$result = mysql_query($sql);
return $result;
}
i got the parent menu which has the parent id -1 but i want to display the child menu from parent id .. any solution ??

Categories