displaying results and hiding duplicates - php

I have a list of catagores found at the bottom of THIS PAGE
this code:
<?php
include_once('include/article.php');
$article = new article;
$articles = $article->fetch_all();
?>
<?php foreach ($articles as $article) { ?>
<div id="content">
<a href="list.php?id=<?php echo $article['promo_cat']; ?>">
<li class="button"><ul class="pageitem">
<?php echo $article['promo_cat']; ?>
</li></ul></div>
</a>
<?php } ?>
displays the value of the promo_cat field in my mysql table as a list:
as you can see. there are 2 "FREE" fields. How do I edit this code so that it does not show up any duplicates?
I understand that I need to use a DISTINCT feature but im not sure how to. Please help.
thank you.
If you require any more coe from another page then please ask and I will edit this post and add it.

SELECT DISTINCT `promo_cat` FROM mobi WHERE `something` = 'something'
More on DISTINCT from http://forums.mysql.com/

for future reference.
changing my above code to:
<?php
include_once('include/article.php');
$category = new category;
$articles = $category->fetch_all();
?>
<?php foreach ($articles as $article) { ?>
<div id="content">
<a href="list.php?id=<?php echo $article['promo_cat']; ?>">
<li class="button"><ul class="pageitem">
<?php echo $article['promo_cat']; ?>
</li></ul></div>
</a>
<?php } ?>
and adding a new CLASS to my /include/article.php that reads:
class category {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT DISTINCT `promo_cat` FROM mobi");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($promo_cat) {
global $pdo;
$query = $pdo->prepare("SELECT DISTINCT * FROM mobi WHERE `something` = 'something'");
$query->bindValue(1, $promo_cat);
$query->execute();
return $query->fetch();
}
}
Fixed my problem.
thank you.

Related

How can I print one thing when table1.id=table2.id, and another thing 'if else'?

I have a menu where some pages have sub-pages which open the drop-down menu.
I have two SQL tables:
'pages' => page_id, page_name;
'subpages' => subpage_id, page_id, page_name;
Subpages get same page_id as the chosen parent page has, when I insert them via my form.
The problem is, all menu elements show the drop-down arrow - even the ones without sub-pages.
How can I print something only if the page has sub-pages?
This is what i want:
if pages.page_id=subpages.page_id
print
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
<i class="fa fa-caret-down"></i>
</button>
else print
<?php echo $page['page_name']; ?>
index.php:
<?php foreach ($pages as $page) { ?>
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-container">
<?php foreach ($subpages as $subpage) {
if($subpage['page_id'] == $page['page_id']) {
?>
<a href="../subpage.php?id=<?php echo $subpage['subpage_id']; ?>">
<?php echo $subpage['subpage_name']; ?>
</a>
<?php } } ?>
</div>
<?php } ?>
Any help is appreciated!
First of all I would change Subpage class like this:
class Subpage {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages");
$query->execute();
return $query->fetchAll();
}
// Static function: Fetch all subpages of single page
public static function fetch_all_page($page_id){
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages WHERE page_id = ?");
$query->bindValue(1, $page_id);
$query->execute();
return $query->fetchAll();
}
public function fetch_data($subpage_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages WHERE subpage_id =
?");
$query->bindValue(1, $subpage_id);
$query->execute();
return $query->fetch();
}
}
And your index.php will become:
<?php foreach ($pages as $page) { ?>
<?php $subpages = Subpage::fetch_all_page($page['page_id']); ?>
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
<?php if (count($subpages)): ?>
<i class="fa fa-caret-down"></i>
<?php endif; ?>
</button>
<div class="dropdown-container">
<?php foreach ($subpages as $subpage): ?>
<a href="../subpage.php?id=<?php echo $subpage['subpage_id']; ?>">
<?php echo $subpage['subpage_name']; ?>
</a>
<?php endforeach; ?>
</div>
<?php } ?>

Display data from three tables with common id in foreach

So I currently have three tables like this given below:
AI = auto increment.
ci_users: user_id(AI), username, slug, email, biography.
ci_terms: term_id(AI), title, slug, body.
ci_relationship: id(AI), term_id, user_id, type.
I'm trying to display all the users from a specific term_id(where type is skill),
for example, let's say term_id 3(where term_id has the title of 'CSS' from ci_terms).
To make it more clear,the above paragraph basically says show me all users with skill of CSS but I want the displayed users to show all of their other skills which are stored in the ci_relationship table.
PARAGRAPHS BELOW ARE AFTER CLICKING A SPECIFIC TERM_ID(SKILL).
This is the function which shows me all of the users that have the term_id as their skill.(from my previous Stackoverflow question. Thanks to pradeep who helped me solve that query:
// All users with specific skill
public function get_user_skill($id)
{
$this->db->select('*, count(ci_relationship.term_id)');
$this->db->from($this->table);
$this->db->join('ci_relationship', 'ci_relationship.user_id = ci_users.id', 'INNER');
$this->db->order_by('ci_relationship.user_id', 'DESC');
$this->db->group_by('ci_relationship.user_id');
$this->db->where('ci_relationship.term_id', $id);
$this->db->where('ci_relationship.type', 'skill');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
}
As I said before the code above just display the users with the specific term_id. Now this is the method in the controller which shows the expected data(users with specific term_id):
This is my controller :
public function skill($id){
// Get data
$data['users'] = $this->User_model->get_user_skill($id);
$term = $this->Terms_model->get($id);
// Get skill name
$data['skill'] = $this->Terms_model->get($id);
// Get skills per foreach --- HERE IS WHERE I NEED HELP
$data['skills'] = $this->User_model->skills($id);
// Meta
$data['title'] = $this->settings->title.' | '. ucfirst($term->title);
// Load template
$this->template->load('public', 'default', 'users/skill', $data);
}
This is the method that I'm trying to create to displayed the term_id per user_id:
// Skill for user foreach
public function skills($id){
$this->db->select('*');
$this->db->from($this->relationship);
$this->db->where('term_id', $id);
$this->db->where('type', 'skill');
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
if($query->num_rows() >= 1){
return $query->result();
} else {
return false;
}
}
The view is the following code:
<?php if($users) : ?>
<div class="row">
<?php foreach($users as $user) : ?>
<div class="col-lg-3 col-sm-6">
<div class="card text-center panel">
<div class="cardheader panel-heading" style="background: url(<?php if($user->user_id) : echo base_url('assets/img/users/covers/'.get_username($user->user_id).'/'.get_username_cover($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>)no-repeat center center;"></div>
<div class="avatar">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><img class="img-circle" alt="<?= get_username($user->user_id); ?> profile picture" title="<?= get_username($user->user_id); ?> profile picture" src="<?php if($user->user_id) : echo base_url('assets/img/users/avatars/'.get_username($user->user_id).'/'.get_username_avatar($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>"></a>
</div>
<div class="info panel-body">
<div class="title">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><?= get_username($user->user_id); ?></a>
</div>
<div class="desc"><?php if(get_occupation($user->user_id)) : ?><?= get_occupation($user->user_id); ?><?php else : echo 'No Job'; endif ?> <?php if(get_company($user->user_id)) : ?> - <b><?= get_company($user->user_id); ?></b><?php else : echo '- <b>No Company</b>'; endif ?></div>
<!-- BEGINNING OF HERE IS WHERE I NEED HELP -->
<?php if($skills) : ?>
<?php foreach($skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>
<!-- END OF HERE IS WHERE I NEED HELP -->
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<div class="alert alert-danger">No user with <?= $skill->title ?> skill found</div>
<?php endif; ?>
This is the actual output:
And this is the expected output:
I hope I could explain myself well enough to make it easy to understand,thanks in advance.
Hope this will help you :
Create a helper method get_skill_by_user just like get_username or get_occupation and use it in your view
function get_skill_by_user($user_id, $id)
{
$ci = & get_instance();
$ci->db->select('*');
$ci->db->from('ci_relationship');
$ci->db->where('term_id', $id);
$ci->db->where('user_id', $user_id);
$ci->db->where('type', 'skill');
$ci->db->order_by('id', 'DESC');
$query = $ci->db->get();
if($query->num_rows() > 0)
{
return $query->result();
} else
{
return false;
}
}
In your view your skills loop should be like this :
<?php $user_skills = get_skill_by_user($user->user_id, $user->term_id); if($user_skills) : ?>
<?php foreach($user_skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>

Category product menu bar not linking

I made a category menu bar on my website so you can click on a category and see products of that category, but its not working for me and I must be doing something wrong.
Some database information:
categories table:
Row 1: id
Row 2: name
In the products table:
Row: category_id
I used a db helper (db_helper.php)
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
This is the my Product_model file where I made the get_categories function:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
?>
And this is the menu bar in my view file where I'm loading all my categories.
<div class="container-fluid">
<div class="row">
<div class="col-lg-1">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<a href="<?php echo $category->id; ?>"><?php echo $category['name']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</div>
I am able to echo all the categories from my database but the links are not working.
I hope someone can help me,
Thanks!
Looks like you are creating a nonsense URL for the href value. Unless the 'id' field of the 'categories' table contains values that use a "controller/method/id" scheme the links you are creating don't go anywhere.
The 'id' field probably just contains numbers. Yes?
If so, your links probably look something like http://example.com/123 which isn't going to go anywhere unless you have a controller named "123".
You need hrefs that go somewhere. I recommend using the anchor() function of Codeigniter URL Helper. You will need to load that helper before this example will work.
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo anchor("controller_name/controller_method/{$category->id}", $category['name']); ?>
</li>
<?php endforeach; ?>
Without the anchor() function it would be written
<?php echo $category['name']; ?>
Try this:
<li class="list-group-item">
<a href="<?php echo $category->id;?>">
<?php echo $category['name']; ?>
</a>
</li>

this is my code. I want my search engine to turn the results into links to another page. idk how to

<?php
require_once 'db/connect.php';
if(isset($_GET['keywords'])) {
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("
SELECT title
FROM articles
WHERE body LIKE '%{$keywords}%'
OR title LIKE '%{$keywords}%'
");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results
</div>
<?php
if($query->num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->title; ?>
</div>
<?php
}
}
}
It appears that all you are querying for is the article title? if this is all you need for the url then just replace the
<?php echo $r->title; ?>
with
<?php echo $r->title; ?>
however if you need an id or something else then you will need to ensure your database query is returning that data for example
SELECT ID, Title FROM...
then use
<a href="http://example.com/<?php echo $r->id; ?>">...

displaying data from mysql

Having a little trouble displaying data from a table. Been looking at my code for the past few hours and can't seem to see the problem. What I am trying to do is when a team is clicked on it will go into the players table and display any player that has that team name on the team page. I keep getting a blank page:
index.php
This is the case that launches the team_view.php
case 'view_team':
$team = $_GET['name'];
$teams = get_players_by_team($team);
include('team_view.php');
break;
team_view.php
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<div id="content">
<h1>Team Roster</h1>
<!-- display product -->
<?php include '../../view/team.php'; ?>
<!-- display buttons -->
</div>
<?php include '../../view/footer.php'; ?>
team.php
<?php
$team = $team['name'];
$first = $player['first'];
$last = $player['last'];
$age = $player['age'];
$position = $player['position'];
$team = $player['team'];
?>
<table>
<?php foreach ($players as $player) :
?>
<tr>
<td id="product_image_column" >
<img src="images/<?php echo $player['player_id']; ?>_s.png"
alt=" ">
</td>
<td>
<p>
<a href="?action=view_player&player_id=<?php echo
$player['player_id']; ?>">
<?php echo $player['first']; ?>
<?php echo $player['last']; ?>
</a>
</p>
</td>
</tr>
<?php endforeach; ?>
</table>
product_db.php
<?php
function get_players_by_team($team) {
global $db;
$query = 'SELECT * FROM players
WHERE team = :team';
try {
$statement = $db->prepare($query);
$statement->bindValue(':team', $team);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
return $result;
} catch (PDOException $e) {
display_db_error($e->getMessage());
}
}
You never define $players it looks like what you are expecting to be $players is actually $teams.
$teams = get_players_by_team($team);
Additionally youre using $player at the top of the script instead of inside the loop which makes no sense.

Categories