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; ?>
Related
I have a database with products and categories tables. I have managed to show the product category and It's related products. And It's currently looks like following.
But I need this to be done like
Category B
Product 1
Category A
Product 2
Product 3
Product 4
Product 5
Following is my query
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row["name"];?></h1>
<p><?php echo $row["prod_desc"];?></p>
<?php echo $row["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]; ?>
<?php
}
}
?>
Rearrange array first then try to use it for showing data
<?php
$final_array = [];
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$final_array[$row["name"]][] = ['prod_desc'=>$row["prod_desc"],'prod_name'=>$row["prod_name"],'quantity'=>$row["quantity"],'price'=>$row["buy_price"]];
}
?>
<?php foreach($final_array as $key=>$val){?>
<h1><?php echo $key;?></h1>
<?php foreach($val as $v){?>
<p><?php echo $v["prod_desc"];?></p>
<?php echo $v["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]."<br><br>"; ?>
<?php } ?>
<?php } ?>
<?php }?>
Try to store last used category and check if it changed:
<?php
$category='';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
if($category!=$row["name"]) {
?>
<h1><?php echo $row["name"];?></h1>
?>
}
$category=$row["name"];
<?php
<p><?php echo $row["prod_desc"];?></p>
<?php echo $row["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]; ?>
<?php
}
}
?>
Remember to sort output data by category in SQL query
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");
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>
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; ?>
I have a query that selects 3 rows; but I only want to display the one row at a time, use the data from that row, then move to the next row, then repeat. How would this be accomplished? Table has 4 items in it.
PHP:
<?php
$server = "secret";
$user = "secret";
$pass = "secret";
$db = "secret";
$con=mysqli_connect($server,$user,$pass,$db);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = 'SELECT * FROM news ORDER BY id DESC LIMIT 3';
mysqli_select_db($con, $db);
$query = mysqli_query($con, $sql);
$number = 1;
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
}
mysqli_close($con);
?>
Attempt to display:
<div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post; ?></p>
<?php
$number++;
?>
Output of attempt:
Test
||
7/14/13
News 1
Testing to see if this will work lalalalalla
NOTE: Tried to repeat the attempt to see if it would work, but it displayed same as output but with a second one that was a duplicate except said News 2
Desired output look:
Newest Title | Newest Date
Newest news. Etc. Insert Big Paragraph of news here etc etc.
2nd Newest Title | 2nd Newest Date
2nd Newest news. Etc. Insert Big Paragraph of news here etc etc.
3rd Newest Title | 3rd Newest Date
3rd Newest news. Etc. Insert Big Paragraph of news here etc etc.
The problem is that you are overwriting your variables with each while() loop -
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
}
so when you try to echo $id, $title, $news_date, and $post, you will only get the last row data
you either need to add your html in the while loop -
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
?>
<div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post; ?></p>
<?php
$number++;
}
or save them to an array
$id = array();
$title = array();
$news_date = array();
$post = array();
while ($result = mysqli_fetch_array($query)) {
$id[] = $result['id'];
$title[] = $result['title'];
$news_date[] = $result['news_date'];
$post[] = $result['post'];
}
and then access the array in your display
<div name='title'><?php echo $title[$number-1]; ?></div> || <div name='news_date'><?php echo $news_date[$number-1]; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post[$number-1]; ?></p>
<?php
$number++;
?>
I used [$number-1] as I assume $number is starting at 1, and arrays are 0 based
edit here are ways to echo your html. It would be beneficial to visit the php documentation, as all of this is there. In these I set $number back to 0, but you can change it back to 1, and just change each of the $number->$number-1, and the $number+1->$number
using a for loop - http://php.net/manual/en/control-structures.for.php
<?php
for($number=0;$number<count($title);$number++){
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
}
?>
using a foreach loop - http://php.net/manual/en/control-structures.foreach.php
<?php
foreach($title as $number => $value{
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
}
?>
using a while loop - http://php.net/manual/en/control-structures.while.php
<?php
$number = 0;
while($number<count($title){
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
$number++;
}
?>
each of the above was done by closing out of php, and writing the html, and then opening php again. You could also just stay in php, and echo the html - http://php.net/manual/en/function.echo.php
<?php
for($number=0;$number<count($title);$number++){
echo "<div name='title'>".$title[$number]."</div> || <div name='news_date'>".$news_date[$number]."</div>";
echo "<p>News ".($number+1)."</p>";
echo "<p>".$post[$number]."</p>";
}
?>