Displaying content but not the latest one - php

I have these codes to display content on my website.
Index.php
$rest = new rest;
$list = $rest->fetch_all();
<?php foreach ($rest->fetch_all() as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
And include.php
class rest {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM podcast ORDER BY cast_id DESC");
$query->execute();
return $query->fetchAll();
} }
Please can someone tell me how I can get this to show results but not the latest result?
All fields are numbered using an ID tag in mysql.
On my site I have the latest entry listed above this list so I do not require the latest one appearing here.
Please help.
Thank you.

The easiest way to do this is to discard the row in the application. You can do it in SQL by using a calculated limit. Calculate it as (SELECT COUNT(*) FROM podcast) - 1.
Or:
SELECT *
FROM podcast
EXCEPT
SELECT TOP 1 *
FROM podcast
ORDER BY cast_id ASC
Excluding the last row.

You can either use array_shift or array_pop depending on your query sorting as shown bellow:
Assuming the latest result is the first raw on your query result.
$rest = new rest;
$list = $rest->fetch_all();
$latest = array_shift($list);
<?php foreach ($list as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
If it's the last raw that you need to hide, then use array_pop

Related

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

How to echo each row from mySQLi query using PHP?

The following query returns all rows from the campaign table that have the user_id of 1:
SELECT * FROM campaign WHERE user_id=1
In the case of testing this is two results. How would I be able to echo a set column from each of the rows. For example, I want to echo the campaign_name from each of the results. I have tried various methods however, I have had no success.
My end goal would be something like this:
<?php foreach($queryRow as $row) { ?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php } ?>
I'm quite at lost with this so I apologise if my intended result is completely off...
Try this:
$qry = "SELECT * FROM campaign WHERE user_id=1";
$res = mysqli_query($conn, $qry);
if(mysqli_num_rows($res) > 0) // checking if there is any row in the resultset
{
while($row = mysqli_fetch_assoc($res)) // Iterate for each rows
{
?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php
}
}
It will iterate for each row in the resultset.
I looked into the documentation as Oldskool kindly suggested and realised I was using the wrong method to create an array. I instead use the fetch_all feature of the mysqli_results class to create a multidimensional array with my results. Then I was able to use the following code to echo out the results:
<!DOCTYPE html>
<html>
<body>
<?php
include('inc/database_initiation.php');
//print_r($userCampaigns);
foreach ($userCampaigns as $row) {
//echo $row[2];
//echo $row[4];
echo $row['campaign_name'];
echo '<br>';
echo $row['promotion_coins'];
echo '<br>';
}
?>
</body>
</html>
The include 'inc/database_initiation is as follows'
<?php
session_start();
include_once 'userManagement/dbconnect.php';
if(!isset($_SESSION['userSession']))
{
header("Location: login.php");
}
$query = $MySQLi_CON->query("SELECT * FROM users WHERE user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$campaignQuery = $MySQLi_CON->query("SELECT * FROM campaign WHERE user_id=1");
$userCampaigns = $campaignQuery->fetch_all(MYSQLI_ASSOC);
//$MySQLi_CON->close();
?>

Formatting dynamically generated checkboxes?

I've got a system that gathers information out of XML files, for the exporter I have been trying to create a system where it gathers the unique imprints from a SQL table then outputs them & uses it for the export query so we can export only what we are after at that time & not everything.
The code I have does the above fine however I've been trying to format it to sit within a table and every 7/8 iterations it starts a new table row however I've been unsuccessful at this point, with 167 rows currently I want it to look a bit better than a massive list (and easier to use).
The code I have currently is the following:
<table>
<form action="exporttrade.php" method="post">
<b>Please Select imprints you wish to Export for <client></b><br />
<?PHP
mysql_connect('server', 'user', 'password');
mysql_select_db("database");
$sql ="SELECT distinct imprint FROM table";
$results= mysql_query($sql);
while( $imprint = mysql_fetch_assoc($results) ):
?>
<tr><td><?php echo $imprint['imprint']; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint['imprint']; ?>"/></td></tr>
<?php endwhile; ?>
I've been attempting to use a counter and modulo function I've seen online to insert code every nth line (in this case will be the ending < /tr> tag. Does anyone here know how you can sucessfully do what I am after? I'm just a bit stumped at how to format this currently :/
Edit - Just a heads up managed to get this working as I wanted it to now used the following:
$imprints=grab_array($sql);
foreach ($imprints as $imprint){
$imprint=$imprint['imprint'];
$counter++;
if($counter % 8 == 0){
echo "</tr><tr>";
}
?>
<td><?php echo $imprint; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint; ?>"/></td>
<?PHP
}
?>
Try this code.
<table>
<form action="exporttrade.php" method="post">
<b>Please Select imprints you wish to Export for <client></b><br />
<?PHP
mysql_connect('server', 'user', 'password');
mysql_select_db("database");
$count = 0;
$sql ="SELECT distinct imprint FROM table";
$results= mysql_query($sql);
while( $imprint = mysql_fetch_assoc($results) ):
if($imprint)
{
count++;
}
while($count <= 8)
?>
<tr><td><?php echo $imprint['imprint']; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint['imprint']; ?>"/></td></tr>
<?php
$count = 0; echo '<tr><td> New Row after 8 </td> </tr>'?>
<?php continue ?>
<?php endwhile; ?>
<?php endwhile; ?>
you can use this logic.

Add related games within a category

I've been trying to add a section called: "related games", in it there is a script (related.php) that will fetch 5 random related games of the same category that the online game is displayed on.
I tried this (related.php):
<?php
if(isset($_GET['genre'])){
$game_category = $_GET['genre'];
$select_games = "SELECT * FROM games ORDER BY rand() LIMIT 0,5";
$run_games = mysql_query($select_games);
while($row = mysql_fetch_array($run_games)){
$game_id = $row['game_id'];
$game_name = $row['game_name'];
$game_image = $row['game_image'];
?>
<table>
<tr>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span></a>
</div>
<tr>
</table>
<?php } } ?>
This is the "related.php" file and I tried to implement it in the following file called: "game_page.php" which works perfectly...
For some reason no random game is showing under the current played game...
Any idea?
I fixed it xD, I simply removed the
if(isset($_GET['genre'])){
$game_category = $_GET['genre'];
strings because they are already included in the "game_page.php" file :)

List won't show up correctly when integrated with PHP

I made a database that has a points system, and to convert this to ranks I planned on showing the data from the database from highest to lowest, then using a list to display it with numbers. However, when displaying it as a list it just shows up as:
1.
1.
1.
1.
1.
1.
(going on until it reaches 10)
Which is not how it should act, I have tried changing the stuff up around the code and the results have only worsened. Can anyone help?
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td><ol><li style='list-style-type:decimal;'> ".$row['points']."</li></ol></td> <td> </td>
<td> </td>
<td> </td></tr>";
}
?>
As mentioned in the comments remove your
<td>
tags or do a table instead of an ordered list and better use ' instead of " for surrounding html tags with attributes in it and use the " for the attribute values, because then you get valid html and no problems in php.
Correct code for list would be:
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
echo "<ol>";
while($row = mysqli_fetch_array($mw2ranks))
{
echo '<li style="list-style-type:decimal;"> '.$row['points'].'</li>';
}
?>
</ol>
For a table you could use the following:
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
?>
<table>
<tr>
<?php
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td> ".$row['points']."</td>
}
?>
</tr>
</table
Your HTML is broken. You can't include the <td> elements inside your <ol>. You need to create a structure like this:
<td>
<ol>
<li> Some stuff </li>
<li> Some stuff </li>
<li> Some stuff </li>
<li> Some stuff </li>
</ol>
</td>
Grouping your <li> elements like this will allow the numbering to work, but obviously it will mess up your table layout. Your alternative is to generate the numbering in your PHP script and not use the ordered list structure at all.
you could use this
<table>
<tr>
<?php
$i=1;
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td> ".$i."</td>";
echo "<td> ".$row['points']."</td>";
$i++;
}
?>
</tr>

Categories