MySQL/PHP Only Returning One Result - php

I have seen a few topics like this so i would like to say sorry first but none of the solutions given in those topics fixed my issue.
I am trying to make a dynamic navigation menu using php and mysql, i have multiple inputs in the database as dummy text but for some reason it only wants to show one result..
Here is my code:
<?php
require_once("../includes/connect.php");
include("../includes/header.php");
include("../includes/functions.php");
?>
<div id="content">
<table id="table">
<tr>
<td id="nav">
<ul class="info">
<?php
$result = mysql_query("SELECT * FROM information LIMIT 10", $connection);
while ($row = mysql_fetch_assoc($result)){
echo "<li>{$row["menu"]}</li>";
$result = mysql_query("SELECT * FROM pages WHERE information_id ={$row["id"]} LIMIT 10", $connection);
echo "<ul class=\"pages\">";
while ($row = mysql_fetch_assoc($result)){
echo "<li>{$row["menu"]}</li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="main">
<h2>Main Content</h2>
</td>
</tr>
</table>
</div>
<?php
include("../includes/footer.php");
?>
</body>
</html>
Dont hate because there are tables in here lol, its not for public use and i will be converting it to grids when i get it all set up and functioning. I am not a 'pro' php programmer so be nice!

I guess the problem is here..
your are pushing datas to the same variables twice....
make first query and push to $result. again make another query and push datas to same var $result.
try pushing it to seperate variable
$result1 = mysql_query("SELECT * FROM pages WHERE information_id ={$row['id']} LIMIT 10", $connection);
echo "<ul class=\"pages\">";
while ($row1 = mysql_fetch_assoc($result)){
echo "<li>{$row1["menu"]}</li>";
}

Related

How to retrieve/display multi columns single row from database to different html tags

OK so what I'm am trying to do is retrieve multiple columns of data from a single row in mysql database and basically place the data of the different columns into multiple places on my html code such as in "p" and "td" tags to display on webpage.
I have successfully been able to display the "description"(description is the name of 1 of the database columns) inside the "p" tags using PHP but I don't know how to display any other columns of the database into other html tags such as the "td" tags. There is probably going to be a total of roughly 10 columns of text in database I want to display in the webpage but again where it's displaying is in various html tags and not a table which is what I keep finding when researching and tired of banging my head on the desk. There is a small example below. I'm learning as I go so any help would be great. Thank you!
<?php
$sql = "SELECT * FROM Parts;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
?>
<div class="description">
<h3>Descrption</h3>
<p>
<?php
if($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['Description'];
}
}
?>
</p>
</div>
<td>
<?php
if($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row["Material"];
}
}
?>
</td>
Try this
<?php
$sql = "SELECT * FROM Parts;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<div class="description">
<h3>Descrption</h3>
<p>
<?php
echo $row['Description'];
?>
</p>
</div>
<td>
<?php
echo $row["Material"];
?>
</td>
<?php
}
}
?>
My preference is to fetch all rows first, rather then fetching rows while rendering html, otherwise if there is an error fetching rows, it becomes mixed in with your html
I also recommend using htmlentities() to escape any html data (assuming that description or material does not already include valid html
<?php
$sql = "SELECT * FROM Parts;";
$result = mysqli_query($conn, $sql);
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
?>
<div class="description">
<h3>Descrption</h3>
<p>
<?php
foreach ($rows as $row) {
echo htmlentities($row['Description']);
}
?>
</p>
</div>
<td>
<?php
foreach ($rows as $row) {
echo htmlentities($row["Material"]);
}
?>
</td>

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)

Issue with bootstrap grid and mysql response

I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections

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>

Displaying information from a database in a loop

On my website i currently have an events page. Each event is read and displayed using a database. for each row in the database i have created a new div like:
<body>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$result = mysql_query('SELECT * FROM test ORDER BY timestamp DESC LIMIT 3;') ?>
<?php while($row = mysql_fetch_assoc($result)) : ?>
<div class="aboutUsContenttest">
<div class="boxheader"><?php echo $row['a'] ?><br></div>
<div class="aboutUsContentText"> <?php echo $row['b'] ?></div>
</div>
<?php endwhile ?>
</body>
this displays 3 different events.....
what i want to do is have an option to "Read More" meaning that i could click on a link which takes me to a separate page with more information about each event. how would i achieve this for say the second event? how would it know to take the information from the second row or div which has been created?
i hope this makes some sort of sense
Thank you
Pass the parametr in the address:
$limit = isset($_GET['view_all']) ? "" : "LIMIT 3";
$result = mysql_query("SELECT * FROM test ORDER BY timestamp DESC {$limit};");
Just have a page named the value of $row['b'] with a php file extension, and create an anchor with the href set to '$row['b'].php'
or, you could add the name of the page as a field in your database.
<body>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$result = mysql_query('SELECT * FROM test ORDER BY timestamp DESC LIMIT 3;') ?>
<?php while($row = mysql_fetch_assoc($result)) : ?>
<div class="aboutUsContenttest">
<div class="boxheader"><?php echo $row['a'] ?><br></div>
<div class="aboutUsContentText"> <?php echo $row['b'] ?></div>
<div><?php echo 'Read More'?></div>
</div>
<?php endwhile ?>
</body>

Categories