html auto rowspan in while - php

i have this result of sql:
SQL:
+-name-+---count---+
+ cat + 5 +
+ dog + 6 +
+------------------+
count of cat is 5 and count of dog is 6.
i want to create table with auto rowspan from this result
in this code rowspan must be get count of cat and get count of dog
HTML:
<table>
<th>NAME</th>
<th>COUNT</th>
WHILE ($sql=mysql_fetch_array($result))
{
<tr>
<td rowspan='$sql['cnt']'> $sql['name']; </td>
<td> $cnt++; </td>
<tr>
$cnt++;
}
</table>

Your code and images do not match, as they are reversed. Also, you are mixing php code with html.
try something like-
WHILE ($sql=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td rowspan="'.$sql['count'].'" valign="center">'.$sql['name'].'</td>';
for($i=1;$i<=$sql['count'];$i++){
echo '<td>'.$i.'</td>';}
echo '<tr>';
}

Related

How Can I nest two foreach to get right values output?

I have two foreach blocks to get some values from a web page (I'm scraping values from an HTML table).
<?php
include('../simple_html_dom.php');
$html = file_get_html('http://www.betexplorer.com/soccer/belgium/jupiler-league/results/');
foreach($html->find('td') as $e) {
echo $e->innertext . '<br>';
}
foreach( $html->find('td[data-odd]') as $td ) {
echo $td->attr['data-odd'].PHP_EOL;
}
?>
and this is my HTML code:
<tr class="strong">
<td class="first-cell tl">
Waasland-Beveren - Anderlecht
</td>
<td class="result">
1:0
</td>
<td class="odds best-betrate" data-odd="5.97"></td>
<td class="odds" data-odd="4.21"></td>
<td class="odds" data-odd="1.51"></td>
<td class="last-cell nobr date">21.02.2016</td>
</tr>
<tr class="">
<td class="first-cell tl">
Waregem - KV Mechelen
</td>
<td class="result">
2:3
</td>
<td class="odds" data-odd="1.83"></td><td class="odds" data-odd="3.71"></td>
<td class="odds best-betrate" data-odd="3.99"></td>
<td class="last-cell nobr date">21.02.2016</td>
</tr>
In this way, in my output, I get before values from the first foreach and, after, values from the second. I'd like to get values together in the right order. For example:
21.02.2016 Waasland-Beveren - Anderlecht 1:0 5.96 4.20 1.51
21.02.2016 Waregem - KV Mechelen 2:3 1.83 3.71 3.98
If they have the exact (count) or (length). Use the normal for after you assign them to two variables.
$td = $html->find('td');
$attr = $html->find('td[data-odd]');
for($i=0; $i < count($td); $i++)
echo $td[$i]->innertext."<br/>".$attr[$i]->attr['data-odd'].PHP_EOL;
Update:
You want to reorder the tds you received from the HTML file, that means you have to think of another logic in how to retrieve them. This updated code is very specific to your case:
$match_dates = $html->find("td[class=last-cell nobr date]"); // we have 1 per match
$titles = $html->find("td[class=first-cell tl]"); // 1 per match
$results = $html->find("td[class=result]"); // 1
$best_bets = $html->find("td[class=odds best-betrate]"); // 1
$odds = $html->find("td[class=odds]"); // 2
// Now to output everything in whatever order you want:
$c=0; $b=0; // two counters
foreach($titles as $match)
echo $match_dates[$c]->innertext." - ".$match->innertext." [".$results[$c]->innertext."] - Best bet rate: ".$best_bets[$c++]->attr['data-odd']." - odds: ".$odds[$b++]->attr['data-odd'].", ".$odds[$b++]->attr['data-odd']."<br/>";

Assign variable to array values and populate <td>

I have the following code inside a WHILE loop following a MySQL query:
$values=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
echo "<tr>
<td class='results'>$values</td>
<td class='results'>working query</td>
</tr>"
I need the $values variable to populate and increase by 1 for each row obtained from the query.
Desired result:
1 | data
2 | data
3 | data
You didn't post the while loop, so I just assume how it looks like. You can just add a simple counter variable and increase for every row returned from DB:
$i = 1;
while ($row = mysqli_fetch_assoc($res)) {
echo "<tr>
<td class='results'>$i</td>
<td class='results'>working query</td>
</tr>";
$i++;
}
use that code please
<table>
<?php
$values=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
foreach($values as $v){
echo "<tr>
<td class='results'>$v</td>
<td>|</td>
<td class='results'>working query</td>
</tr>";
}
?>
</table>

How to display results in two columns using PHP?

I trying to display the product details into 2 columns.
At the moment, my below PHP is displaying as a single columm. Can someone please help? Thanks
Brand A Brand B
Brand C Brand D
not
Brand A
Brand B
Brand C
Brand D
<?php
$count = 0;
while($record = mysql_fetch_array($results)){
$id = $record["prod_id"];
$brand = $record["prod_brand"];
$name = $record ["prod_name"];
$nameC = $record ["prod_name_c"];
$price = $record ["prod_price"];
if($count % 2 <> 0) {
extract($record);
echo '<td class="itemHotDesc" id="bfTable3" valign="top"><a href="productDetail.php?id='.$id.'"><img src="admin/img/' . $id . '.jpg" class="itemImgborder" /></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="itemHotDesc" align="center">';
echo $brand;
echo '</br>';
echo $name;
echo '</br>';
echo $nameC;
echo '</br>';
echo 'Special: US$'.$price;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo "<td> </td>";
echo '</tr>';
}
else
{
echo '<td class="itemHotDesc" id="bfTable3" valign="top"><a href="productDetail.php?id='.$id.'"><img src="admin/img/' . $id . '.jpg" class="itemImgborder" /></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="itemHotDesc" align="center">';
echo $brand;
echo '</br>';
echo $name;
echo '</br>';
echo $nameC;
echo '</br>';
echo 'Speical: US$'.$price;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo "<td> </td>";
echo '</tr>';
}
$count++;
}
?>
Your question is not PHP related, but HTML related.
To have a table with multiple columns, you need to have a <table> element and one <tr> element per row that you want to have. In each <tr> you need to have one <td> element per column that you want to have.
If you have just one <td> in each <tr>, you will not get multiple columns.
Here is an example of a table with multiple columns:
<table>
<tr>
<td>This is row 1, Column 1</td>
<td>This is row 1, Column 2</td>
<td>This is row 1, Column 3</td>
</tr>
<tr>
<td>This is row 2, Column 1</td>
<td>This is row 2, Column 2</td>
<td>This is row 2, Column 3</td>
</tr>
<tr>
<td>This is row 3, Column 1</td>
<td>This is row 3, Column 2</td>
<td>This is row 3, Column 3</td>
</tr>
</table>
This should give you something like
-------------------------------------------------------------------------------
| This is row 1, Column 1 | This is row 1, Column 2 | This is row 1, Column 3 |
|-------------------------|-------------------------|-------------------------|
| This is row 2, Column 1 | This is row 2, Column 2 | This is row 2, Column 3 |
|-------------------------|-------------------------|-------------------------|
| This is row 3, Column 1 | This is row 3, Column 2 | This is row 2, Column 3 |
-------------------------------------------------------------------------------

How to categorized results from SQL, display 6 columns and set colorize on every category

I would like to ask for help on how to achieve this. I have this code below that pull records from DB and display it in 6 columns.
What I want to achieve is that, I want to display results on 6 columns but I want to categorize and set different color on every category.
let say i want to display the whole set of fruits starting from letter A with 6 columns with colors of gray, then below all letters starting with B with 6 columns with white color on background then below is C with gray colors in 6 columns. thanks.
<?php
fruits = $stmt->prepare("SELECT * FROM fruits ORDER by fruit_id ASC");
$fruits->execute();
$cols = 6;
do {
echo "<tr>";
for ($i = 1; $i <= $cols; $i++) {
$row = $fruits->fetch(PDO::FETCH_ASSOC);
if ($row) {
$fruit_id = $row['fruit_id'];
$fruit_name = $row['fruit_name'];
?>
<td>
<table>
<tr valign="top">
<td>
<?php echo '<input type="checkbox" id="fruit_id[]" name="fruit_id[]" value="' . $fruit_id . '"/>' . $fruit_name . "\n"; ?>
</td>
<td width="30"> </td>
</tr>
</table>
</td>
<?php
} else {
echo "<td> </td>";
}
}
} while ($row);
?>
Do it in the database:
SELECT fruits.*,if(#evenodd>0,'grey','white') AS color, #evenodd:=-1*evenodd AS dummy FROM (#evenodd:=1) as init, fruits ORDER by fruit_id ASC

Mysql fetch array, table results 2

I've recently posted a similar question but that was to create one row of cells across and after it reaches 3 columns, it creates a new row, so I can have 3 columns of infinite rows. That was solved.
Now what I need is this (GIVEN A and B should be records from the database using $row = mysql_fetch_array($results) so basically it would be something like $row['username']; for A and B ).
<tr>
<td><img src="images/ava/A.png" /></td>
<td>A</td>
<td width="2px" rowspan="3"></td>
<td><img src="images/ava/B.png" /></td>
<td>B</td>
</tr>
<tr>
<td><div class="gauge"><div class="innergauge"></div>A</div></td>
<td><div class="gauge"><div class="innergauge"></div>B</div></td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
As what you can see, the record I got from my database has to fill in to this format, where A is one record and B is another, then if there is record C, this whole thing should repeat again to form a second row.
Build each section as you go, combine them at the end. Here's how you do sections 1 and 3, the middle section is left as an exercise:
$section1 = '<tr>';
$section2 = // start $section2
$section3 = '<tr>';
$i = 0;
while($row = mysql_fetch_array(...)){
if($i > 0){
// won't happen the first time through the loop
$section1.= '<td width="2px" rowspan="3"></td>';
}
$section1 .= '<td><img src="images/ava/'.$row['username'].'"/></td>';
$section1 .= '<td>'.$row['username'].'</td>';
// now do $section2
$section3 .= '<td>'.$row['username'].'</td>';
$i++;
}
$section1 .= '</tr>';
// finish $section 2
$section3 .= '</tr>';
// now output
echo $section1 . $section2 . $section3;

Categories