This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to iterate by row through a mysql query in php
I need to iterate thorough the mysql rows. For an example, if question id is 1 all the answers have to display under the question 1. I am a beginner to php , can any one please help me??
<?php
$result = select("SELECT * FROM questions WHERE question_id=1");
$row = mysql_fetch_array($result);
?>
<table width="581" height="299" border="1">
<tr>
<td>Union Assurance Questionnaire</td>
</tr>
<tr>
<td>1.<?php echo $row['questions']; ?>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<?php
$result = select("SELECT * FROM questions WHERE question_id=2");
$row = mysql_fetch_array($result);
?>
<table width="581" height="299" border="1">
<tr>
<td>2.<?php echo $row['questions']; ?>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
use like this
<?php
while($row = mysql_fetch_array($result))
{
?>
<table width="581" height="299" border="1">
<tr>
<td>Union Assurance Questionnaire</td>
</tr>
<tr>
<td>1.<?php echo $row['questions']; ?>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<?php
}
?>
You have to use while() loop for iterating all possible answers.
Related
first question here, my problem is this.
I've got 22 more or less values to print stored inside a table in a DB. Problem is some of this values gotta have HTML tags in it, mainly tags <s> .
<?php
$query = "SELECT * FROM teams";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "
<div class='col-md-12'>
<div class='card strpied-tabled-with-hover'>
<div class='card-header '>
<h4 class='card-title'>".$row['name']." ".$row['surname']."</h4>
<p class='card-category'>".$row['team']."</p>
</div>
<div class='card-body table-full-width table-responsive'>
<table class='table table-hover table-striped'>
<thead>
<th>#</th>
<th>Player</th>
<th>Player Bonus</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td style='color:red; font-weight:bold'>".$row['c1']."</td>
<td>".$row['c1b']."</td>
</tr>
<tr>
<td>2</td>
<td>".$row['c2']."</td>
<td>".$row['c2b']."</td>
</tr>
<tr>
<td>3</td>
<td>".$row['c3']."</td>
<td>".$row['c3b']."</td>
</tr>
<tr>
<td>4</td>
<td>".$row['c4']."</td>
<td>".$row['c4b']."</td>
</tr>
<tr>
<td>5</td>
<td>".$row['c5']."</td>
<td>".$row['c5b']."</td>
</tr>
<tr>
<td>6</td>
<td>".$row['c6']."</td>
<td>".$row['c6b']."</td>
</tr>
<tr>
<td>7</td>
<td>".$row['c7']."</td>
<td>".$row['c7b']."</td>
</tr>
<tr>
<td>8</td>
<td>".$row['c8']."</td>
<td>".$row['c8b']."</td>
</tr>
<tr>
<td>9</td>
<td>".$row['c9']."</td>
<td>".$row['c9b']."</td>
</tr>
<tr>
<td>10</td>
<td>".$row['c10']."</td>
<td>".$row['c10b']."</td>
</tr>
<tr>
<td>11</td>
<td>".$row['c11']."</td>
<td>".$row['c11b']."</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>";} ?>
TL;DR: $row[c1] and so on have HTML tags stored as values like <s>stringvalue</s> but it just echo stringvalue. What can I do? Thanks and sorry for my inexperience.
EDIT: I mispelled the title in an horrific way.
I have two HTML tables. Each has three rows and one column only. I'd like to join them programmatically such that I get one table with two columns and three rows.
Is there some function or hack to achieve this? How can I achieve this?
For example
First Table:
<table id="table_one">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
Second Table:
<table id="table_two">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
This is what I want from the above two tables:
<table id="table_three">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
Well, this is a way to proceed (check in-code comments):
$table1 = <<<'_DATA_'
<table id="table_one">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
_DATA_;
$table2 = <<<'_DATA_'
<table id="table_two">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
_DATA_;
// Load teh 1st table
// Create a DOM object
$html1 = new simple_html_dom();
// Load HTML from a string
$html1->load($table1);
// Load teh 2nd table
// Create a DOM object
$html2 = new simple_html_dom();
// Load HTML from a string
$html2->load($table2);
// Find all rows from both tables
$rows1 = $html1->find('tr');
$rows2 = $html2->find('tr');
// Build teh 3rd table
$table3 = '<table id="table_three">
<tbody>';
// Insert rows cells from both initial tables
for ($i=0; $i < count($rows1); $i++) {
$table3 .= '<tr>';
// get row's innerhtml
$table3 .= $rows1[$i]->innertext;
$table3 .= $rows2[$i]->innertext;
$table3 .= '</tr>';
};
// finish the table
$table3 .= '</tbody>
</table>';
// Clear DOM objects
$html1->clear();
unset($html1);
$html2->clear();
unset($html2);
It creates this:
<table id="table_three">
<tbody>
</tbody>
</table>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#table_one tr').each(function(index){
var row = $(this);
var row2 = $('#table_two tr').get(index);
$(row).append($(row2).find('td'));
$('#table_three').append($(row));
});
});
</script>
Here's how I'd do it:
// assume we have the tables in three strings:
// table_one in $str_1, table_two in $str_2, and table_three in $str_3.
// create $str_3 by copying $str_1 and replacing the table ID
$str_3 = $str_1;
$str_3 = str_replace('table_one', 'table_three', $str_3);
// load table_two and table_three in simple-html-dom and collect the TD elements
$tab_2 = str_get_html($str_2);
$tab_3 = str_get_html($str_3);
$tds_3 = $tab_3->find("td");
$tds_2 = $tab_2->find("td");
// go through the TDs in table_three and append the corresponding table_two TD
$acc = 0;
foreach ($tds_3 as &$td) {
$td->outertext = $td->outertext . $tds_2[$acc]->outertext;
$acc++;
}
// done!
echo $tab_3;
Not sure I understood the question, but:
<?php
// first and second table
//Select data
$reponse = $bdd->prepare('SELECT data1, data2 FROM '.RESEARCH.' WHERE X=X');
$reponse->execute(array(X));
?>
//Begin table
<table id="table_one_or_two">
<tbody>
//Loop the rows
while ($data = $reponse->fetch())
{
<tr>
<td>
<label>$data['data1']</label>
</td>
</tr>
}
//end of loop, end of table
</tbody>
</table>
// 3TH table
//Select data
$reponse = $bdd->prepare('SELECT data1, data2 FROM '.RESEARCH.' WHERE X=X');
$reponse->execute(array(X));
?>
//Begin table
<table id="table_three">
<tbody>
//Loop the rows
while ($data = $reponse->fetch())
{
<tr>
<td>
<label>$data['data1']</label>
</td>
<td>
<label>$data['data2']</label>
</td>
</tr>
}
//end of loop, end of table
</tbody>
</table>
I'm trying to get this output from returned data fetched from mysql:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
<td>LCD</td><td>Yes</td><td>No</td>
<td>Auto Pilot</td><td>No</td><td>No</td>
<td>Fast Generation</td><td>Yes</td><td>No</td>
<td>Dual Cores</td><td>No</td><td>Yes</td>
</tr>
</tbody>
</table>
But I have trouble getting the following code to achieve that output with one foreach loop. It uses in_array to check whether each value from $featured_tests exists in the returned data.
$featured_tests = array("LCD","Auto Pilot","Fast Generation","Dual Cores");
$table_head ="<table><thead><tr><th></th>";
$table_colspan1 = "</tr></thead><tbody><tr><td colspan='5'>Features</td></tr>";
$table_end ="</tr></tbody></table>";
foreach($rows as $row)
{
$special_features = explode(",",$row->special_features);
$header_image .= "<th><img src='".$row->image_url."'></th>";
foreach($featured_tests as $featured_test)
{
$featured .= "<tr><td>".$featured_test."</td>";
if(in_array($featured_test,$special_features))
{
$featured .= "<td>Yes</td>";
}
else
{
$featured .= "<td>No</td>";
}
}
}
$table_html = $table_head.$header_image.$table_colspan1.$featured.$table_end;
But the result I'm getting is a mess. Each value in $featured_tests is iterating over and over again for each product and thus results in a very long table. Can anyone help me correct my code to get the ideal output?
Here's the result:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
</tr>
<tr>
<td>LCD</td><td>Yes</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>Yes</td>
</tr>
<tr>
<td>Dual Cores</td>No<td>
</tr>
<tr>
<td>LCD</td><td>No</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>No</td>
</tr>
<tr>
<td>Dual Cores</td>Yes<td>
</tr>
</tbody>
</table>
you are creating new rows <tr> inside on most deep foreach...
take this example to make what you want:
<table>
<thead>
<th>Col 1</th><th>Col 2</th>
</thead>
<tbody>
<?php foreach($large_array as $less_array): ?>
<tr>
<?php foreach($less_array as $row): ?>
<!-- <td> contents etc</td>-->
<?php endforeach?>
</tr>
<?php endforeach;?>
</tbody>
</table>
I get that error in php..And I have no idea why...
Here is how my connection is established:
<?php
$hostname_QASite = "localhost";
$database_QASite = "qasite";
$username_QASite = "root";
$password_QASite = "";
$QASite = mysql_pconnect($hostname_QASite, $username_QASite, $password_QASite) or trigger_error(mysql_error(),E_USER_ERROR);
?>
my query is the following:
mysql_select_db($database_QASite, $QASite) or die(mysql_error());
$query_get_all_topics = "SELECT topic_id, title FROM topic";
$get_all_topics = mysql_query($query_get_all_topics, $QASite) or die(mysql_error());
$row_get_all_topics = mysql_fetch_assoc($get_all_topics);
$totalRows_get_all_topics = mysql_num_rows($get_all_topics);
And then I iterate over the row_get_all_topics ...
What is wrong in the code ?
Edit:
I get that error, when I try to loop 2 times over different results in the database.
UPDATE:
<body>
<br/><br/><br/><br/><br/><br/><br/>
<div align="center">
<ul id="navlist">
<li> צור נושא</li>
<li> ראה קשרים</li>
</ul>
<?php do { ?>
<table border="1">
<tr>
<td>
<table width="100%" border="1" >
<tr>
<td width="90%" align="right">
<?php echo $row_get_all_topics['title']; ?>
</td>
<td width="10%">
:שם נושא
</td>
</tr>
<tr>
<td colspan="2">
<table>
<tr>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<?php
//$result=$mysql_query("SELECT title, sub_topic_id FROM sub_topic WHERE topic_id=".$row_get_all_topics['topic_id']) or die(mysql_error());
$result="";
if($row=mysql_fetch_array($result))
{
do
{
?>
<table >
<tr>
<td>
<?php echo $row['title']; ?>
</td>
<td>
:תת נושא
</td>
</tr>
<tr>
<td colspan="2">
<table>
<tr>
<td>
</td>
</tr>
<tr>
<td>
עדכן
</td>
<td>
מחק
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
<?php
**}while($row=mysql_fetch_assoc($result));** 1 FIRST LOOP
}//end suptopic search
?>
<?php **} while ($row_get_all_topics = mysql_fetch_assoc($get_all_topics)); ?>** 2ND LOOP
AS soon as I add this line, to query teh database inside the loop, the page shows the error..
$result=$mysql_query("SELECT title, sub_topic_id FROM sub_topic WHERE topic_id=".$row_get_all_topics['topic_id']) or die(mysql_error());
I've been struggling with this for around 4 hours now...
What I'm trying to establish is pretty simple, I have a news table, I want to display the title of the news, the content, and a read more link, I know how to loop through a table and force , but this won't work in my case, the table should look like this in the end:
<table>
<tr>
<td>header one</td>
<td>header 2</td>
<td>header 3</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td>text one</td>
<td>text two</td>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
<td>read more </td>
<td>read more</td>
</tr>
</table>
What I have so far in my php is a code that will generate the rows and columns, but I want them to be distributed just like the sample table above in order not to mess the alignment of the text and the read more link ...
Here's my php code :
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<?php while ($record = mysql_fetch_assoc($result)): ?>
<?php $style++ ?>
<td width="33%" valign="top">
<h6><?php echo $record['title'] ?></h6>
<div class="service-sum"><?php echo $record['content'] ?></div>
<div class="findout">> find out more</div>
</td>
<?php if ($style == 3): ?>
</tr>
<?php $style = 0; ?>
<tr>
<td>
<div style="height:30px;"></div>
</td>
</tr>
<tr>
<?php endif ?>
<?php endwhile ?>
This one is working fine, but i'm displaying the title and the content and the read more link in one column, these should be distributed into 3 for design purposes...
Any help would be much appreciated, I looked all over the net and I can't find a solution for that!
try using this alignment. The design is almost same as yours.
<table>
<tr>
<td>
<table>
<tr>
<td>header one</td>
</tr>
<tr>
<td>text one</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 2</td>
</tr>
<tr>
<td>text 2</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 3</td>
</tr>
<tr>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
</tr>
</table>
if there are only 3 records, the following should be fine. if there are more, some changes should be made. Please check your php code.
<table>
<tr>
<?php while ($record = mysql_fetch_assoc($result)) { ?>
<td>
<table>
<tr>
<td><?php echo $record['title'] ?></td>
</tr>
<tr><td height = "30px;"></td></tr>
<tr>
<td><?php echo $record['content'] ?></td>
</tr>
<tr>
<td>> find out more</td>
</tr>
</table>
</td>
<?php } ?>
</tr>
</table>