Fixed column width with changing no. of columns - php

I have a horizontal table that have a changing number of columns.
How do I set a fixed column width? I have tried to set width in the main table setup and in the tr and td tags. No luck so far.
Any suggestions?
Here is my code
...mysql query
while ($row = mysql_fetch_assoc($result))
{
$file_no .= '<td>'.$row['File_no'].'</td>';
$user .= '<td>'.$row['User'].'</td>';
$movement .='<td>'.$row['Downloaded']. " &nbsp " .$row['Uploaded'].'</td>';
$i++;
}
echo '
<table border="1">
<tbody>
<tr align="center">
<td align="left"><b>File no.</b></td>'.$file_no .'
</tr>
<tr align="center">
<td align="left"><b>User</b></td>'.$user .'
</tr>
<tr align="center">
<td align="left"><b>Movement</b></td>'.$movement .'
</tr>
</tbody>
</table>
';

Related

Simple HTML DOM Parser. Remove first row in table and then remove second column of each

I am trying to parse an HTML string which is a table. I managed to remove the first row of the table. How can I then remove the second column in the table?
I remove the first row like this:
$preparsed = "<div style='border:1px #CCCCCC dotted; padding:5px'><div style=''>
<div id='divPrint'>
<table cellpadding='5' cellspacing='0' width='886'>
<tr bgcolor='#666666' class='normal' style='color:#FFFFFF; font-weight:bold;'>
<td width='100px' style='font-size:11px;'><b>Type</b></td>
<td width='110px' style='font-size:12px;'><b>Date</b></td>
<td width='100px' style='font-size:12px;'><b>Details</b></td>
<td width='140px' style='font-size:12px;'><b>Instructor</b></td>
<td width='140px' style='font-size:12px;'><b>Student / Client</b></td>
<td style='font-size:12px;'><b>Comment</b></td>
</tr>
<tr class='normal' style='font-size:11px' bgcolor='#EEEEEE'>
<td style='font-size:12px;'>Training</td>
<td style='font-size:12px;'>2016-10-05 <br /><i class='small'>(16:00:00-18:00:00)</i></td>
<td style='font-size:12px;'>Zara</td>
<td style='font-size:12px;'>Gary</td>
<td style='font-size:12px;'>Alfred</td>
<td style='font-size:12px;'></td>
</tr><tr class='normal' style='font-size:11px' bgcolor='#FFFFFF'>
<td style='font-size:12px;'>Training</td>
<td style='font-size:12px;'>2016-10-05 <br /><i class='small'>(12:00:00-15:00:00)</i></td>
<td style='font-size:12px;'>Zara</td>
<td style='font-size:12px;'>Gary</td>
<td style='font-size:12px;'>shawn</td>
<td style='font-size:12px;'></td>
</tr>
</table>
</div>
</div></div>";
$html = new simple_html_dom();
$html->load($preparsed);
$rows = array_slice($html->find('tr'), 1);
foreach ( $rows as $element ) {
echo '<h3>'. $element->plaintext . '</h3>';
This removes the first row. How can I either a)First remove every second column in each row (the date), or b) remove the first row first and then remove all the second columns in each row?
Like this:
$html = new simple_html_dom();
$html->load($preparsed);
$rows = array_slice($html->find('tr'), 1);
foreach ( $rows as $element ) {
echo '<h3>'. $element->plaintext . '</h3>';
$cols = array_slice($element->find('td'), 2);
foreach ( $cols as $col ) {
echo '<h4>The second col os the row values '. $col->plaintext . '</h4>';
}
}

Show Data Horizontally in Table using PHP & MySQL

I'm trying to display data horizontally in a table by using PHP & MySQL. The code that I'm currently using is (Obviously all values will be called from MySQL but currently I have put static values):-
$query = "select * from bonusdetails where BonusType='Free Money' order by Validity ASC limit 0,3;";
$result = mysql_query($query);
echo '<table width="150" border="0" cellspacing="0" cellpadding="0" bgcolor="#941010" style="font-family:Arial, Helvetica, sans-serif; float:left; color:#ffffff;">';
while ($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td align="center" style="padding:2px 0 0 0;"><img src="abc.jpg" width="124" height="64"/></td>';
echo '</tr>';
echo '<tr>';
echo '<td align="center" style="font-size:15px; padding:8px 0 0 0;">No Deposit Bonus</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="font-size:17px; padding:5px 0 0 0;" align="center" >CODE: STAR75</td>';
echo '</tr>';
echo '<tr>';
echo ' <td align="center" style="padding:8px 0 5px 0;"><a href="#"><input name="more-details" type="button" style="background:url(images/more-details.png) no-repeat; width:102px;
height:27px; text-decoration:none; border:none; cursor:pointer;" /></a></td>';
echo '</tr>';
}
echo '</table>';
I'm getting output in this format: http://www.casinobonustips.com/submitbonus/bonus.php. is there a way to display the same data horizontally?
I will highly appreciate your assistance on this.
Thanks and Regards,
Devjeet
What you have:
<table>
<tr>
<td>A1</td>
</tr>
<tr>
<td>B1</td>
</tr>
<tr>
<td>C1</td>
</tr>
<tr>
<td>A2</td>
</tr>
<tr>
<td>B2</td>
</tr>
<tr>
<td>C2</td>
</tr>
<tr>
<td>A3</td>
</tr>
<tr>
<td>B3</td>
</tr>
<tr>
<td>C3</td>
</tr>
</table>
What you should have:
<table>
<tr>
<td>
<div>A1</div>
<div>B1</div>
<div>C1</div>
</td>
<td>
<div>A2</div>
<div>B2</div>
<div>C2</div>
</td>
<td>
<div>A3</div>
<div>B3</div>
<div>C3</div>
</td>
</tr>
</table>
Example: http://jsfiddle.net/ePMmx/1/
Do your styling to suit your needs.
You need to group <td></td> in one <tr></tr> to display them "horizontally", on the other hand, you are mixing "view" stuff with processing and retrieving the data, you maybe have to read about MVC pattern for better programming

print php result in a nice html table

my aim is to pull data from mysql and print it in html table. asumming that 1,2,3...8 are the data
<table style="width: 100%">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
</table>
this is the code i have so far but this will only print out the column but no the row. plz help. thank you
<table style="width: 100%; color:aqua">
<?php
$showFoto = getFoto();
echo '<tr>';
foreach($showFoto as $Foto){
echo '<td class="afs"><img alt="" src="img/'.$Foto['img'].'.'.$Foto['ext'].'"><br>'.$Foto['about'].'</td>';
}
echo '</tr>';
?>
</table>
TRY
<table width ="100%" style="color:aqua" cellpadding="2" cellspacing="2">
<tr>
<?php
$showFoto = getFoto();
$i=0;
foreach($showFoto as $Foto){
++$i;
echo ($i%4==0) ? '</tr><tr>' :'';
echo '<td class="afs">
<img alt="" src="img/'.$Foto['img'].'.'.$Foto['ext'].'">'.$Foto['about'].
'</td>';
}
?>
</tr>
</table>

Alternate multiple columns in an html table with php

How can I output an alternate multiple column like this
<table border="1">
<tr>
<td> userid 1 </td>
</tr>
<tr>
<td> userid 2</td> <td>userid 3 </td>
</tr>
<tr>
<td> userid 4 </td>
</tr>
<tr>
<td> userid5 </td> <td>userid6 </td>
</tr>
<tr>
<td> userid7 </td>
<td>userid8 </td>
<td> userid9 </td>
<td>userid10</td>
</tr>
</table>
<br>
<table border=1>
My table query is like this:
$result = mysql_query("SELECT id,name FROM `tbl-record`") or die(mysql_error());
Classic example in action is like this:
http://www.cashcashpinoy.com
A table with five rows and an alternate 1x2x1x2x4 columns (TD ) on each rows ( TR )
Since this is in a table, you can use the colspan attribute on the td elements, like this:
<table>
<tr>
<td colspan="4">Full width data.</td>
</tr>
<tr>
<td colspan="2">Half width data.</td>
<td colspan="2">Half width data.</td>
</tr>
<tr>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
</tr>
</table>
You can do this with any number of columns.
If you want to assign a number of columns dynamically, you'll want to have a set number of results to provide some consistency, which you could do using a LIMIT along with your query.
$results; // This is all of the results of your query
$colOps = array(1,2,4); // Different colspan values
$numCols = 4; // Maximum columns to allow per line
echo '<table border="1">';
while(count($results) > 0) {
$ind = mt_rand(0, 2); // Generate a random index number
if(count($results) >= $colOps[$ind]) {
echo '<tr>';
for($i = 0; $i < $cols; $i++)
echo '<td colspan="'.$colOps[$ind].'">'.array_shift($results).'</td>';
echo '</tr>';
}
}
echo '</table>';
Note that this is untested and may need some modification to work properly.

Loop through html table (for each tr)

I need to be able to loop through an HTML table and output data. In each <tr> there is 8 td's. The first td is a dropdown menu of engineers. The next 7 tds are days of the week with drop downs for time slots.
I'm basically building a scheduler output for a specific internal app. (that's irrelevant here).
So, here's an example table:
<table width="200" border="1">
<tr>
<th scope="col">Engineer</th>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
<th scope="col">Thursday</th>
<th scope="col">Friday</th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
<tr>
<td>John Doe</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Jane Doe</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Each td has a form element and each tr is a "section" of data. In the end I will have 20+ tr's and need to be able to run through each tr and grab the relavant data from these, however, I need to be able to iterate through each tr so that I can manage the code better.
Is there a way to do this with PHP?
echo '<table width="100%" border="0"><tr>';
echo '<td width="20px"></td>';
echo '<td align="left"><strong>Title</strong></td>';
echo '<td align="center" width="125px"><strong>Posted</strong></td>';
$sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM `announcement` ORDER BY `id` DESC LIMIT '.$search['start'].', '.$search['max'];
$rows = $mysql_conn->fetch_array($sql);
foreach($rows as $key=>$record) {
echo (($key+1)%2) ? '<tr bgcolor="#AEDEFF" >' : '<tr>';
echo '<td align="left"><input class="checkbox" type="checkbox" name="delete[]" id="delete[]" value="'.$record["id"].'" /></td>';
echo '<td align="left">'. $record["title"] .'</td>';
echo '<td align="center">'.$record["datetime"].'</td></tr>';
}
echo '</table>';
Example of what I use when I want to output a list of rows with columns of data. Not sure if this is what you want but it works. :)

Categories