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.
Related
I'm trying to give structure to my code and i am facing a problem.
I'm looping through a sql query response and for each element i'm trying to retrieve other related elements. It works in my controller without problem but when i'm trying to repeat in the view I always get the same value for the related element
My controller:
<?php
include_once('class/guide.class.php');
$bdd = new DBHandler();
$req = guide::getGuides($bdd,0,5);
foreach ($req as $results => $poi)
{
$req[$results]['id'] = htmlspecialchars($poi['id']);;
$req[$results]['name'] = nl2br(htmlspecialchars($poi['name']));
$guide = new guide($results['name'],$bdd);
$guidePois = $guide->getGuidePois($poi['id']);
foreach ($guidePois as $res => $re)
{
echo $guidePois[$res]['id'];
echo $guidePois[$res]['name'];
$guidePois[$res]['id'] = htmlspecialchars($re['id']);
$guidePois[$res]['name'] = nl2br(htmlspecialchars($re['name']));
}
}
include_once('listing.php');
here, you see that I echo the ids/names of the related list of element and it works well, the output is correct for each element of the first list.
When i do it in my view:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Somehow the first list output are the good elements, but for the 2nd list, i always get the related elements of the first item.
Do you have an idea ?
Thanks a lot for your help
This is because you only set:
$guidePois = $guide->getGuidePois($poi['id']);
once in the controller.
If you want it to work in the view, you need to insert this code right after the closing </h3>
<?php $guidePois = $guide->getGuidePois($poi['id']); ?>
So that $guidePois gets a new value in each iteration.
Complete view code:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php
$guidePois = $guide->getGuidePois($poi['id']);
foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
I have 2 SQLite- tables, one containing articles and one containing image URL's.
What I'd like to do is to do a foreach() to show the articles in order and show a set number of random images next to the article.
The images should be selected by checking similarity between the Article's and image's categories (column in the table) and then randomized. (The article catergory could be for example 'motorboats' and the img category 'boat').
How do I show the results from two different arrays?
The code for the articles is:
$stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="artikelLista">
<?php foreach($res as $article): ?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?>
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
To add the second array, I tried this, which didn't work, it only showed the results from the first array multiple times:
$stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt2 = $db->prepare('SELECT * FROM Object;');
$stmt2->execute();
$res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="artikelLista">
<?php foreach($res as $article): ?>
<?php foreach($res2 as $object): ?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?><br>
<?php echo $object['img'];?> <!-- For testing. The images should be filtered and a a few random images would be shown here -->
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
I guess it's not possible to use nested foreach() like this.
How will I manage this?
Also, If anyone knows on top of their head how to match the similarities between the arrays as described above, I'd be greatful. If not, I'll deal with this later.
You can fall a function inside the first foreach a pass category of the article to that function. Now in that functino collect all images in an array related
to the passed category and then return it by randomizing it. Code below ..
<?php foreach($res as $article): ?>
<?php
$img = get_img($article['category']);
?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?><br>
<img src="<?php echo $img; ?>" /> <!-- The return result will be shown here -->
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
<?php
function get_img($category){
$stmt2 = $db->prepare('SELECT * FROM Object WHERE category = '.$category);
$stmt2->execute();
$res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$rnd = array();
foreach($res2 as $object){
$rnd[] = $object['img'];
}
$n = rand(0,(count($rnd)-1));
return $rnd[$n];
}
?>
I have a SQLite- file with values.
I get those values and display them in a table of 2 columns:
<?php
$db = new PDO("sqlite:$dbPath");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$stmt = $db->prepare('SELECT * FROM Object;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<table id="table1">
<caption><em>caption</em></caption>
<?php foreach($res as $objekt):
$imageFile = basename($objekt['image']); ?>
<tr>
<td><?php if(isset($objekt['image'])): ?>
<figure class="objectPicture center">
<img src="img/bmo/250/<?php echo $imageFile;?>" alt="<?php echo $objekt['title'];?>">
<?php else: ?>no picture.<?php endif ?><br>
<figcaption><?php echo $objekt['title']; ?>
</figcaption>
</figure>
</td>
<td><?php echo $objekt['text']; ?><br>
<?php echo $objekt['owner']; ?></td>
</tr>
<?php endforeach; ?>
</table>
What I'd like to do is to show the next foreach-loop (values of row2 in the SQLite- file) in another 2 columns next to the first result. And the 3rd loop under the first one and so on.
How do I do that? (total newbie to PHP and SQL here, in case you didn't notice)
The results now:
What I want:
3 Changes to make
Add $idx to foreach loop
<?php foreach($res as $idx => $objekt):
Wrap if around <tr>
<?php if($idx % 2 == 0) { ?>
<tr>
<?php } ?>
Wrap if around </tr>
<?php if($idx % 2 == 1) { ?>
</tr>
<?php } ?>
The above code doesn't consider the case when row number is odd
I need to set two variable in a url, $id and $job. The data should come from mysql select statement, [id] and [job_number_id]. My page displays a client job proposal and if there is more than one all proposals are displayed but the [id] and the [job_number_id] determines what is displayed on the webpage. I dont have a clue as to how this done. Any help would be greatly appreciated. Here's the code:
<?php
$url = 'http://localhost/estimate_lar/homepage.php';
$id = $_SESSION['id'];
$query = "SELECT id, client_job_name, job_number_id
FROM `job_name`
WHERE `id`='$id'";
$allJobs = $db->query($query);
?>
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.''; ?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
If you want the variables to be avaialble in the URL you need to read them with $_GET.
Getting the arguements from a url such as index.php?id=1&job_number_id=3 will look like that:
if (isset($_GET['id']) && isset($_GET['job_number_id'])) {//make sure both arguments are set
$id = $_GET['id'];
$job_number_id = $_GET['job_number_id'];
}
To set it in your foreach statement:
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php
$url = "http://localhost/estimate_lar/homepage.php?id=" . $site_title['id'] . "&job_number_id=" . $site_title['job_number_id'];
echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'';
?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
PLEASE remember to read about SQL injection and making sure you are escaping your inputs. Or even better - use a prepared statement.
Currently your script is volunerable, since everyone could just alter the URL and manipluate your DB.
Hope this helps!
Try this.
<?php
session_start();
$id = $_SESSION['id'];
$url = 'http://localhost/estimate_lar/homepage.php';
if($id){
$query = "SELECT id, client_job_name, job_number_id FROM `job_name` WHERE `id`='$id'";
$allJobs = $db->query($query);
}else{
echo "Id not in session";
}
?>
<table>
<?php if ($allJobs) { foreach ($allJobs as $site_title) : ?>
<tr>
<td>
<?php echo $site_title['job_number_id']. " ".$site_title['job_number_id']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php }else{ echo 'No results Found' ;} ?>
This may help you.
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.