Tables are displayed horizontally instead of vertical [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to create an table with PHP and HTML which gets the data from a MySQL database.
The problem is that the data is displayed horizontally and not vertical.
if (isset($_POST['winneron'])) {
echo "<tr>";
while ($printuser = mysql_fetch_array($user)) {
echo "<th>". $printuser['username'] . "</th>";
}
echo "</tr>";
while ($printgames = mysql_fetch_array($games)) {
if ( $printgames ['winner'] == $printgames ['team1'] ) {
echo "<td><b>". strtoupper($printgames ['winner']) . "</b></td>";
}
else {
echo "<td>". strtoupper($printgames ['winner']) . "</td>";
}
}
}
Hope somebody can help me.

If you want your display to be vertical then you have to put each piece of information into a table row <tr><td>...</td></tr>
while ($ausgabespiele = mysql_fetch_array($spiele)) {
if ( $ausgabespiele['sieger'] == $ausgabespiele['team1'] ) {
echo "<tr><td><b>". strtoupper($ausgabespiele['sieger']) . "</b></td></tr>";
}
else {
echo "<tr><td>". strtoupper($ausgabespiele['sieger']) . "</td></tr>";
}
}

There are no <tr> and </tr> in your while ($ausgabespiele = mysql_fetch_array($spiele)) { }...

You are missing TR in while
if (isset($_POST['winneron'])) {
echo "<tr>";
while ($ausgabeuser = mysql_fetch_array($user)) {
echo "<th>". $ausgabeuser['username'] . "</th>";
}
echo "</tr>";
while ($ausgabespiele = mysql_fetch_array($spiele)) {
echo "<tr>";
if ( $ausgabespiele['sieger'] == $ausgabespiele['team1'] ) {
echo "<td><b>". strtoupper($ausgabespiele['sieger']) . "</b></td>";
}
else {
echo "<td>". strtoupper($ausgabespiele['sieger']) . "</td>";
}
echo "</tr>";
}
}

You have to add the row-tags within the while loop...
if (isset($_POST['winneron'])) {
echo "<tr>";
while ($ausgabeuser = mysql_fetch_array($user)) {
echo "<th>". $ausgabeuser['username'] . "</th>";
}
echo "</tr>";
while ($ausgabespiele = mysql_fetch_array($spiele)) {
echo "<tr>";
if ( $ausgabespiele['sieger'] == $ausgabespiele['team1'] ) {
echo "<td><b>". strtoupper($ausgabespiele['sieger']) . "</b></td>";
}
else {
echo "<td>". strtoupper($ausgabespiele['sieger']) . "</td>";
}
echo "</tr>";
}
}

You forget to create a table row insde the loop. To display vertically you need to place the table cells in rows:
while ($ausgabespiele = mysql_fetch_array($spiele)) {
echo "<tr>";
if ( $ausgabespiele['sieger'] == $ausgabespiele['team1'] ) {
echo "<td><b>". strtoupper($ausgabespiele['sieger']) . "</b></td>";
}
else {
echo "<tr><td>". strtoupper($ausgabespiele['sieger']) . "</td></tr>";
}
echo "</tr>";
}

Try this -
if (isset($_POST['winneron'])) {
$echo = "<tr>";
while ($ausgabeuser = mysql_fetch_array($user)) {
$echo .= "<th>". $ausgabeuser['username'] . "</th>";
}
$echo .= "</tr><tr>";
while ($ausgabespiele = mysql_fetch_array($spiele)) {
if ($ausgabespiele['sieger'] == $ausgabespiele['team1'])
$echo .= "<td><b>". strtoupper($ausgabespiele['sieger']) . "</b></td>";
else
$echo .= "<td>". strtoupper($ausgabespiele['sieger']) . "</td>";
}
$echo .= "</tr>";
echo $echo;
}

Related

PHP For Loop jQuery UI Tabs

I am trying to display a list of Links in a table according to Distance and Category. I would like each distance to be a Tab and have the appropriate Links in each Tab. I am trying to accomplish this with A PHP Foreach loop and jQuery-UI Tabs.
Here is the code which gets the data and displays it in the table in each Tab.
The index function in the controller for the View and the function that gets the table data:
public function index() {
$data = array();
$db_name = $this->uri->segment(2);
$this->db->db_select($db_name);
$tables = $this->db->get('tableinfo');
$data['distances'] = array();
$data['tables'] = array(
'men' => array(),
'women' => array()
);
foreach($tables->result() as $row) {
if(!in_array($row->distance, $data['distances'])) {
array_push($data['distances'], $row->distance);
}
if(substr($row->displayname, 0, 4) == "Male") {
array_push($data["tables"]['men'], $row->displayname);
} else {
array_push($data["tables"]['women'], $row->displayname);
}
}
$data['dbname'] = $db_name;
$this->parser->parse('templates/header', $data);
$this->parser->parse('select/index', $data);
$this->parser->parse('templates/footer', $data);
}
public function gettable($table) {
$db_name = "championchipco_sowetomarathon2019";
$this->db->db_select($db_name);
redirect("results/index/" . $db_name . "/" . $table);
}
And the View:
<?php
$i = 1;
echo "<div class='row'>";
echo "<div class='col' id='tabs'>";
echo "<ul>";
foreach($distances as $distance) {
echo "<li><a href='#tabs-" . $i . "'>" . $distance . "</a></li>";
}
echo "</ul>";
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}
echo "</div>";
echo "</div>";
?>
At the moment, all of the data is being displayed in every Tab, instead of only display the Links for the particular category in a different tab. I can see that the 2nd table of Men and Women is slightly to the left of the top one so I think the loop is causing something to go wrong.
I have tried re-arranging the way the loops display the data in the View but cannot seem to get only the 10KM in the 10KM Tab, 21KM in 21KM Tab, etc.
Get the data of your second foreach by Ajax it will made your need simple
I mentioned to remove below foreach
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}

Trying to generate a html file from PHP code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a PHP program from OpenEMR that prints context to the console in HTML format. I wanted to save whatever it prints to another html file. I am not very familiar with PHP and front end technologies. Does anyone know a simple way to make it work? Thanks a lot.
Below is the code detail:
foreach($newpatient as $patient){
/*
$inclookupres = sqlStatement("select distinct formdir from forms where pid='".$pids[$iCounter]."'");
while($result = sqlFetchArray($inclookupres)) {
include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
}
*/
print "<div id='superbill_patientdata'>";
print "<h1>".xlt('Patient Data').":</h1>";
printRecDataOne($patient_data_array, getRecPatientData ($pids[$iCounter]), $N);
print "</div>";
print "<div id='superbill_insurancedata'>";
print "<h1>".xlt('Insurance Data').":</h1>";
print "<h2>".xlt('Primary').":</h2>";
printRecDataOne($insurance_data_array, getRecInsuranceData ($pids[$iCounter],"primary"), $N);
print "<h2>".xlt('Secondary').":</h2>";
printRecDataOne($insurance_data_array, getRecInsuranceData ($pids[$iCounter],"secondary"), $N);
print "<h2>".xlt('Tertiary').":</h2>";
printRecDataOne($insurance_data_array, getRecInsuranceData ($pids[$iCounter],"tertiary"), $N);
print "</div>";
print "<div id='superbill_billingdata'>";
print "<h1>".xlt('Billing Information').":</h1>";
if (count($patient) > 0) {
$billings = array();
echo "<table width='100%'>";
echo "<tr>";
echo "<td class='bold' width='10%'>".xlt('Date')."</td>";
echo "<td class='bold' width='20%'>".xlt('Provider')."</td>";
echo "<td class='bold' width='40%'>".xlt('Code')."</td>";
echo "<td class='bold' width='10%'>".xlt('Fee')."</td></tr>\n";
$total = 0.00;
$copays = 0.00;
//foreach ($patient as $be) {
$ta = split(":",$patient);
$billing = getPatientBillingEncounter($pids[$iCounter],$ta[1]);
$billings[] = $billing;
foreach ($billing as $b) {
// grab the date to reformat it in the output
$bdate = strtotime($b['date']);
echo "<tr>\n";
echo "<td class='text' style='font-size: 0.8em'>" . oeFormatShortDate(date("Y-m-d",$bdate)) . "<BR>" . date("h:i a", $bdate) . "</td>";
echo "<td class='text'>" . text($b['provider_name']) . "</td>";
echo "<td class='text'>";
echo text($b['code_type']) . ":\t" . text($b['code']) . " ". text($b['modifier']) . " " . text($b['code_text']) . " ";
echo "</td>\n";
echo "<td class='text'>";
echo oeFormatMoney($b['fee']);
echo "</td>\n";
echo "</tr>\n";
$total += $b['fee'];
}
// Calculate the copay for the encounter
$copays = getPatientCopay($pids[$iCounter],$ta[1]);
//}
echo "<tr><td> </td></tr>";
echo "<tr><td class='bold' colspan=3 style='text-align:right'>".xlt('Sub-Total')."</td><td class='text'>" . oeFormatMoney($total + abs($copays)) . "</td></tr>";
echo "<tr><td class='bold' colspan=3 style='text-align:right'>".xlt('Copay Paid')."</td><td class='text'>" . oeFormatMoney(abs($copays)) . "</td></tr>";
echo "<tr><td class='bold' colspan=3 style='text-align:right'>".xlt('Total')."</td><td class='text'>" . oeFormatMoney($total) . "</td></tr>";
echo "</table>";
echo "<pre>";
//print_r($billings);
echo "</pre>";
}
echo "</div>";
<?php
ob_start();
/*
YOUR CODE
*/
$page= ob_get_contents();
if ($fp = fopen('../page.htm', 'w+')) {
fwrite ($fp, $page);
fclose($fp);
}
ob_end_flush();
?>

Show specific fields in array in php?

echo "<table>";
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
echo "<tr>\n";
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
I want to show only the first 4 fields in the table. I am decoding a multidimensional array in php. I
Try like
$cnt = 0;
foreach($arr['chart_data'][$x] as $key=>$val) {
if($cnt++ < 4) {
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
} else {
break;
}
}

Format text to fit in columns in Excel

I have this block of text in an array:
"Stefan Olsson"
"Kungsvägen"
"Skolgatan"
xxxx-xx-xx
0735xxxxxx,
"Pär Davidsson"
"Skolgatan"
"Myntvägen"
xxxx-xx-xx
0709xxxxxx,
I parse this type of content to an CSV-file, for later usage in Excel. However, I want to fromat this text to fit in different columns in excell. So, when I open the CSV-file in Execel, I want the name to be in one column, the address in the column besides etcetc. How can I accomplish this? Should I use PHPExcel? Or could it be done with plain old PHP?
Here is my PHP-code
$gatunamn = $_POST['gata'];
$ort = $_POST['omrade'];
$csv_data = array();
$newSpider->fetchPage($gatunamn, $ort, $offset=0);
$obj = json_decode($newSpider->html);
echo "<div id='rightcontent'><table id='one-column-emphasis'>";
echo "<th><input type='checkbox' name='csv_all' id='csv_all'></th><th>Namn</th><th>Adress</th><th>Adress2</th><th>Adress3</th><th>Personnummer</th><th>Telefonnummer</th><th>Telefonnummer2</th>";
$antal_sidor = round($obj->search->wp->totalHits / $obj->search->wp->pageSize);
echo "<td></td>";
foreach($obj->search->wp->features as $fish) //Loopar ut 50st (pageSize)
{
echo "<tr>";
echo "<td><input type='checkbox' value='csv' class='csv'></td>";
echo "<td>" . $fish->name . "</td>";
$csv_data[] .= utf8_decode($fish->name);
foreach($fish->addresses as $ad)
{
echo "<td>" . $ad->label . " " . $ad->postcode . " " . $ad->area . "</td>";
$csv_data[] .= utf8_decode($ad->label . " " . $ad->postcode . " " . $ad->area);
}
if(!empty($fish->dateOfBirth))
{
$convert_date = substr($fish->dateOfBirth, 0, -3); //Gör om datum från timestamp
echo "<td>" . date("Y-m-d", $convert_date) . "</td>";
$convert_datee = date("Y-m-d", $convert_date);
$csv_data[] .= $convert_datee;
}
if(!empty($fish->phoneNumbers))
{
foreach($fish->phoneNumbers as $ph)
{
echo "<td>" . $ph . "</td>";
$csv_data[] .= $ph . ",";
}
}
echo "</tr>";
}
echo "</table>";
$j = 0;
for($i = 1; $i <= $antal_sidor; $i++)
{
echo "<a href='curl2.php?gatunamn=$gatunamn&ort=$ort&offset=$j'>" . $i . "</a> ";
$j += 100;
}
echo "</div>";
echo "<div id='debug'><pre>";
var_dump($csv_data);
echo "</pre></div>";
}
if(isset($_POST['export']))
{
$fp = fopen("eniroo.csv","w");
foreach(explode(",", implode("\n",$csv_data)) as $rad) {
fputcsv($fp, array(implode(',', str_getcsv($rad, "\n"))));
}
echo "<div id='csv_info'>";
echo "<a href='eniro.csv'>Hämta CSV-fil</a>";
echo "</div>";
}
// Restructure the original array into rows
$myDataArray = array_chunk($myDataArray, 5);
// Then write to CSV
$fp = fopen('file.csv', 'w');
foreach($myDataArray as $dataRow) {
fputcsv($fh, $dataRow);
}
fclose($fh)

Print query with loops in it and condition

I have a foreach function which permits me to print fields in a database,
I want to do possible that if $row[value]="photo" don't just print the $row[value] but another thing.
Can I do it?
The code is this:
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
echo "<td>" . $row[$value] . "</td>";
}
I need to have a condition that if $row[value]=photo to echo another thing not: echo "<td>" . $row[$photo] . "</td>";
Any example?
Since i could not get what to print for if($row['value'] == photo) check, so add that code yourself in the code written below.
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}
This doesn't need to be complex.
foreach ($result as $row) {
if ($row['value'] === 'photo') {
// print "another thing"
} else {
// print "something"
}
}
Sure, just include an if statement with that. Your code will be similar to the code below:
$arr = array("one", "two", "three");
foreach ($arr as $value) {
if($value == "one")
echo "Not gonna print one.";
else
echo "Value: $value<br />\n";
}
Using your code, you can see if the value is not photo, echo it.
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
if($row[value] == "photo") {
echo "some other thing";
} else {
echo "<td>" . $row[$value] . "</td>";
}
}
}
If you want to print img src if the field is "photo" field (key) then
Try this :
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($key == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}

Categories