Make a foreach array from mysql SELECT - php

I have the following code:
$result = mysqli_query($con, "SELECT * FROM da_questions");
while($row = mysqli_fetch_array($result))
{
}
mysqli_close($con);
which selects everything from the table da_questions. Now this is in a file called config.php which file is required in all my pages of my website. Now I want to do the following on my external pages... let's say index.php
<?php foreach($results as $question): ?>
<p><?php echo $question['question_title']; ?></p>
<br />
<p><?php echo $question['question_text']; ?></p>
<br />
<?php endforeach; ?>
My file outlay is like shown below:
index.php
config/
config.php
How can I do this?

Why not just
$results = array(); while($row = mysqli_fetch_array($reult)) { $results[] = $row; }
or as #Phil pointed out:
$results = $result->fetch_all(MYSQLI_ASSOC)

With your code
$result = mysqli_query($con, "SELECT * FROM da_questions");
$results = array();
while($row = mysqli_fetch_array($result))
{
$results[] = $row;
}
mysqli_close($con);
in the index.php write as below
include('config/config.php');
<?php foreach($results as $question): ?>
<p><?php echo $question['question_title']; ?></p>
<br />
<p><?php echo $question['question_text']; ?></p>
<br />
<?php endforeach; ?>

Related

Display mysqli result multiple times [duplicate]

This question already has answers here:
How can I use mysqli_fetch_array() twice?
(4 answers)
Closed 3 months ago.
I asked this recently but it got marked as duplicate and deleted.
Please look at my question before marking it down because it is not the same and I am struggling to figure this out.
I want to echo the 'company' and 'area' results from a MYSQLI query into my page, at separate points in the body of a php page.
Only the first echo will show. Please show my mistake.
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Connections/****";
include_once($path);
$dbhandle=mysqli_connect($hostname_Demo, $username_Demo, $password_Demo, $database_Demo);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
?>
<head>
<title>TEST</title>
</head>
<body>
<?php
while($row = mysqli_fetch_assoc($result))
{
echo $row['company'];
}
?>
<?php
while($row = mysqli_fetch_assoc($result))
{
echo $row['area'];
}
?>
<?php $dbhandle->close(); ?>
</body>
</html>
You can use this code
<?php
while($row = mysqli_fetch_assoc($result))
{
$data_array[] = $row;
}
foreach ($data_array as $data) {
echo $data['company'];
}
foreach ($data_array as $data) {
echo $data['area'];
}
Do like this with.., You don't want to have to make more queries to your database.
You are fetching same row two different which is not correct.
Something like this should work
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Connections/****";
include_once($path);
$dbhandle=mysqli_connect($hostname_Demo, $username_Demo, $password_Demo, $database_Demo);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
$row =array();
while ($x= mysqli_fetch_assoc($result))
$row[]=$x;
$i=0;
?>
<head>
<title>TEST</title>
</head>
<body>
<?php
echo $row[i]['company'];
?>
<?php
echo $row[i]['area'];
$i++;
?>
try merging them it looks like they are just the same
<?php
while($row = mysqli_fetch_assoc($result))
{
echo $row['company']."
".$row['area'];
}
?>
This is what I ended with. It works so I hope it is correct. Thanks for everyones help.
$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
$row =array();
while ($x= mysqli_fetch_assoc($result))
$row_RS1=$x;
?>
<?php
$sql_RS2="SELECT * from employees where EmployeeID = 79";
$result=mysqli_query($dbhandle,$sql_RS2);
$row_RS2 =array();
while ($x= mysqli_fetch_assoc($result))
$row_RS2=$x;
?>
<head>
<title>TEST</title>
</head>
<body>
<p>
<?php echo $row_RS1['company']; ?>
</p>
<p>
<?php echo $row_RS1['company']; ?>
</p>
<p>
<?php echo $row_RS1['area']; ?>
</p>
<p>
<?php echo $row_RS2['EmployeeID']; ?>
</p>
<p>
<?php echo $row_RS2['Surname']; ?>
</p>
<p>
<?php echo $row_RS2['EmployeeID']; ?>
</p>
<p>
<?php $dbhandle->close(); ?>
</p>
</body>

Delete first post and show second in PHP

I have a question in PHP.
I am creating a website with posts but I can't make the PHP to show just the second post (without show the first).
My code is like this:
<?php
$result = mysqli_query($dbc, "SELECT * FROM projects");
$x = 1;
while($row = mysqli_fetch_array($result)){
$nome = $row['name'];
$conteudo = $row['description'];
$imagem = $row['image'];
$imagem2 = $row['image2'];
?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<div class="content justify" id="projects-<?php echo $x; ?>" >
<?php echo $conteudo; ?>
<?php if(!empty($imagem2)) { ?>
<img class="hide-for-small" src="images/contebt/project/<?php echo $image2; ?>">
<?php }; ?>
</div>
<?php $count2++; } ?>
<?php $x++;}; ?>
With this code I can show just the first post, but I want to show just the second. Can anybody help me, please? Thanks!
This should work with this code :
PHP
<?php
$result = mysqli_query($dbc, "SELECT * FROM projects");
$x = 0;
while($row = mysqli_fetch_array($result)) {
$nome = $row['name'];
$conteudo = $row['description'];
$imagem = $row['image'];
$imagem2 = $row['image2'];
if (!$x) {
$x = 1;
continue;
}
?>
<div class="content justify" id="projects-<?php echo $x; ?>" >
<?php echo $conteudo; ?>
<?php if(!empty($imagem2)) { ?>
<img class="hide-for-small" src="images/contebt/project/<?php echo $image2; ?>">
<?php } ?>
</div>
<?php
$x++;
}
?>
SQL
But you should directly espace the first row directly in mysql usign either limit :
$result = mysqli_query($dbc, "SELECT * FROM projects LIMIT 1, 1");
or where statement :
$result = mysqli_query($dbc, "SELECT * FROM projects where id > 1");

Trying to display information coming from the database in a list and a div with only one select

Im reading questions and answers columns of my faq table.
And I want to show my faq questions and correspondent answers where status is icual to 1.
I know how can I do this with two selects, but with one select Im not see how it is possible.
Because I canĀ“t do something like my example1: (Because Im repeating my <ol> </ol> in my while loop and dont works great.)
Do you know how can I do this using just one select?
example1:
<div id="container">
<?php
$read = mysql_query("SELECT * FROM faq WHERE status = 1");
$lines = mysql_num_rows($pesquisa);
?>
<h2>FAQ: Answers to <?php echo $lines ?> frequently asked questions.</h2>
<?php
while($result = mysql_fetch_array($read)){
$question = $result['question'];
$answer = $result['answer'];
echo '<ol>';
echo '<li>'.$question.'</li>';
echo '</ol>';
echo '<hr/>';
echo '<div id="answers">';
echo '<h2>'.$question.'</h2>';
echo '<p>'.$answer.'</p>';
echo '</div>';
}
?>
</div>
My code:
<div id="container">
<?php
$read = mysql_query("SELECT * FROM faq WHERE status = 1");
$lines = mysql_num_rows($pesquisa);
?>
<h2>FAQ: Answers to <?php echo $lines ?> frequently asked questions.</h2>
<ol>
<?php
while($result = mysql_fetch_array($read)){
$question = $result['question'];
$answer = $result['answer'];
echo '<li>'.$question.'</li>';
}
?>
</ol>
<hr/>
<div id="answers">
<h2><?php echo $question; ?></h2>
<p><?php echo $answer; ?></p>
</div>
</div>
alright, here is what you looking for;
<div id="container">
<?php
$read = mysql_query("SELECT * FROM faq WHERE status = 1");
$lines = mysql_num_rows($pesquisa);
$results = array();
while($result = mysql_fetch_array($read)){
$results[]=$result;
}
?>
<h2>FAQ: Answers to <?php echo $lines ?> frequently asked questions.</h2>
<ol>
<?php
foreach($results as $result){
$question = $result['question'];
$answer = $result['answer'];
$link='#'.$question;
echo '<li><a href='.$link.'>'.$question.'</a></li>';
}
?>
</ol>
<hr/>
<div id="answers">
<?php
foreach($results as $result){
$question = $result['question'];
$answer = $result['answer'];
echo'<div id='.$question.'>';
echo"<h2>".$question."</h2>";
echo"<p>".$answer."</p>";
}
?>
</div>
</div>
I'm not sure I understand exactly what you are asking but is it something like this?
<div id="container">
<?php
$read = mysql_query("SELECT * FROM faq WHERE status = 1");
$lines = mysql_num_rows($pesquisa);
?>
<h2>FAQ: Answers to <?php echo $lines ?> frequently asked questions.</h2>
<ol>
<?php
while($result = mysql_fetch_array($read)){
$question = $result['question'];
$answer = $result['answer'];
echo '<li>'.$question.'</li>';
}
?>
</ol>
<hr/>
<div id="answers">
<?php
reset($read);
while($result = mysql_fetch_array($read)){ ?>
<h2 name='<?php echo str_replace(" ","",$question); ?>'><?php echo $result['question']; ?></h2>
<p><?php echo $result['answer']; ?></p>
<? }?>
</div>
</div>
If you are just dealing with a formatting issue then don't use the ol tag...you can reproduce any effect you want with simple divs.
Try using an array :
<div id="container">
<?php
$read = mysqli_query("SELECT * FROM faq WHERE status = 1");
$lines = mysqli_num_rows($pesquisa);
$result = mysqli_fetch_all($read); //<- Put all results in one array to work on
?>
<h2>FAQ: Answers to <?php echo $lines ?> frequently asked questions.</h2>
<ol>
<?php
foreach($row in $result) {
echo '<li>'.$row['question'].'</li>';
}
?>
</ol>
<hr/>
<div id="answers">
<?php
foreach($row in $result) {
echo '<h2>'. $row['question'].'</h2>';
echo '<p>'. $row['answer'].'</p>';
}
?>
</div>
</div>

How do I loop through 2 columns?

I've got a table called tickets. The Tickets table has 4 columns: user_id, ticket_id, subject, message. I want to loop the ticket_id and subject of the logged in user. I want to echo it with foreach, but I cant get it to work. This is what I've got:
Query:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = '$user_id'") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($getTickets)){
$showTicketID = $row['ticket_id'];
$showTicketSubject = $row['subject'];
}
Code in the page:
<?php foreach ($showTheTickets as $showTheTickets): ?>
<div class="preview">
<?php echo $showTicketID ?>
<?php echo $showTicketSubject ?>
</div>
<?php endforeach; ?>
Anyones willing to help me? Thanks.
Try this:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = $user_id") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_array($getTickets)){
$row = array( 'ticket_id' => $row['ticket_id'], 'subject' => $row['subject'] );
$showTheTickets[] = $row;
}
And then:
<?php foreach ($showTheTickets as $stt): ?>
<div class="preview">
<?php echo $stt['ticket_id'] ?>
<?php echo $stt['subject'] ?>
</div>
<?php endforeach; ?>
Hope it helps...
You are overwriting the values you get from the query and your $showTheTickets array remains empty.
You probably want something like:
$showTheTickets = array();
while($row = mysqli_fetch_assoc($getTickets)){
$showTheTickets[$row['ticket_id']] = $row['subject'];
}
and:
<?php foreach ($showTheTickets as $id => $subject): ?>
<div class="preview">
<?php echo $id; ?>
<?php echo $subject; ?>
</div>
<?php endforeach; ?>
Why not simply
while($row = mysqli_fetch_assoc($getTickets)){
?><div class="preview"><?
echo $row['ticket_id'];
echo $row['subject'];
?></div><?
}
you have to save $showTicketID and $showTicketSubject in a array (could be the same array or 2 arrays) and then in the view show the arrays, like this:
while($row = mysqli_fetch_assoc($getTickets)){
$array[$row['ticket_id']] = $row['subject'];
}
and the view:
<?php foreach ($showTheTickets as $ticketid => $ticketsubject): ?>
<div class="preview">
<?php echo $ticketid ?>
<?php echo $ticketsubject ?>
</div>
<?php endforeach; ?>

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