How do i put a <td> in my <table> with php? - php

I’m working on a php snippet and i made a table. I tried putting a td tag inside but when i do this, a lot disappears. This is a piece of my code:
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</tr>';
}
}
echo '</tbody></table>';
The code behind the // is where i tried to put the td tag but when i put it there, the images that normally appear go blank and when i inspect my code there is a lot of other code that also disappears. What am i doing wrong here?
Thank you for your help!

First I can see a problem with these lines :
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
Because you just close your first TABLE at the end, but not the other inside :
echo '</tbody></table>';

There are some inconsistency of your <td> to <th>. You can have a look below
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;">
<thead>
<tr>
<th>Product</th>
<th>Naam</th>
<th>Prijs</th>
<th>Qte</th>
</tr>
</thead><tbody>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo "<tr>
<td style='display: flex; border: 1px solid black;'>
<img src='$product->url' class='php_image' style='width: 15%; height: 15%;'/>
</td>
<td>Your product name</td>
<td>$product->description</td>
<td>
<p style='color: green;'>$product->price1</p>
<p style='color: red;'>$product->price</p>
</td>
<td>$product->stock</td>
</tr>";
}
}
echo '</tbody></table>';
?>

There's a lot problems with the code. From what i can see in your code, you have a table inside a tbody and at the end closed only one table.
Secondly you're also trying to put a td inside another td which is not the right thing to do. Check out mozilla developer website for more information on using HTML tables.

//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;"><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</td></tr>';
}
}
echo '</tbody></table>';
You are not closing your tags correctly. Also checkout the docs just as #christopher_bincom has mentioned.

Related

How I can keep track of xml next children to print this html table with correct format using php

I want to fetch some data from database as xml and print them inside a html table using php.
here is my php code:
$xml=simplexml_load_string($ru) or die("Error: Cannot create object");
echo '<table style="border: 1px solid black; border-collapse: collapse;">';
foreach ($xml->employee->children() as $key=>$value){
if($key == 'PAYMSTR_SALHDNM' ){
echo '<tr>';
echo '<th style="border: 1px solid black; border-collapse: collapse;">'.$value.'</th>';
echo '<td style="border: 1px solid black; border-collapse: collapse;">'.$value.'</td>';
echo '</tr>';
}
else{
echo '<tr>';
echo '<th style="border: 1px solid black; border-collapse: collapse;">'.$key.'</th>';
echo '<td style="border: 1px solid black; border-collapse: collapse;">'.$value.'</td>';
echo '</tr>';
}
}
echo '</table>';
Now I am getting this output:
But want to make each value of tag PAYMSTR_SALHDNM as table head [its done ]and each value of tag PAYMSTR_AMOUNT as table data for tag PAYMSTR_SALHDNM
Like the following image:
i think if, somehow i can keep track of next $xml->employee->children() and its value,then i can bring such correct format.Please help me on that.Thanks
Updates: Here is my Table:
Here is my xml:
<employee><id>FMCSC00015</id><year>2016</year><month>1</month><paymstr_salhdnm>BASIC PAY</paymstr_salhdnm><paymstr_amount>35600</paymstr_amount><paymstr_salhdnm>ASSOCIATION SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>240</paymstr_amount><paymstr_salhdnm>TELEPHONE ALLOWANCE</paymstr_salhdnm><paymstr_amount>800</paymstr_amount><paymstr_salhdnm>HOUSE RENT DEDUCTION</paymstr_salhdnm><paymstr_amount>2587.5</paymstr_amount><paymstr_salhdnm>MEDICAL ALLOWANCE</paymstr_salhdnm><paymstr_amount>700</paymstr_amount><paymstr_salhdnm>GAS BILL</paymstr_salhdnm><paymstr_amount>450</paymstr_amount><paymstr_salhdnm>DEARNESS ALLOWANCE</paymstr_salhdnm><paymstr_amount>6000</paymstr_amount><paymstr_salhdnm>LIFE INSURANCE PREMIUM (D)</paymstr_salhdnm><paymstr_amount>1718</paymstr_amount><paymstr_salhdnm>PF SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>3560</paymstr_amount><paymstr_salhdnm>PF ADVANCE LOAN</paymstr_salhdnm><paymstr_amount>2796</paymstr_amount><paymstr_salhdnm>BENEVOLENT FUND</paymstr_salhdnm><paymstr_amount>1780</paymstr_amount><paymstr_salhdnm>HEALTH INSURANCE PREMIUM</paymstr_salhdnm><paymstr_amount>150</paymstr_amount><paymstr_salhdnm>STUDENT WELFARE</paymstr_salhdnm><paymstr_amount>3</paymstr_amount><paymstr_salhdnm>MUNICIPAL TAX & SWEAPER CH.</paymstr_salhdnm><paymstr_amount>18</paymstr_amount><paymstr_salhdnm>STAIR CASE LIGHT CHARGES</paymstr_salhdnm><paymstr_amount>1.25</paymstr_amount><paymstr_salhdnm>CLUB</paymstr_salhdnm><paymstr_amount>207</paymstr_amount><paymstr_salhdnm>TEACHER'S FAMILY WELFARE ASSOC</paymstr_salhdnm><paymstr_amount>10</paymstr_amount><paymstr_salhdnm>MOTOR GARAGE</paymstr_salhdnm><paymstr_amount>500.5</paymstr_amount><paymstr_salhdnm>RESEARCH ALLOWANCE</paymstr_salhdnm><paymstr_amount>1500</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-3</paymstr_salhdnm><paymstr_amount>5728</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-4</paymstr_salhdnm><paymstr_amount>23490</paymstr_amount></employee>
Here's a solution. The basic idea is to…
in a first loop extract all the header data(the first three header items to be pre-initialised),
in a second loop select data and build table, where the loop index is the index into the header table.
Code
<?php
$ru = "..."; // Has pre-defined content
$xml=simplexml_load_string($ru) or die("Error: Cannot create object");
// Collect and create headers
$headers = array('ID', 'YEAR', 'MONTH'); // Pre-define headers
foreach ($xml->paymstr_salhdnm as $key=>$header) {
$headers[] = (string)$header;
}
// Create data table
$hindex=0;
echo '<table style="border: 1px solid black; border-collapse: collapse;">';
foreach ($xml->children() as $key=>$value){
if (strtolower($key) == 'paymstr_salhdnm' ) continue; // header data, so ignore
echo '<tr>';
echo ' <td style="border: 1px solid black; border-collapse: collapse;">'.((isset($headers[$hindex])) ? $headers[$hindex] : ' ').'</td>';
echo ' <td style="border: 1px solid black; border-collapse: collapse;">'.$value.'</td>';
echo '</tr>';
$hindex++;
}
echo '</table>';
?>
Result
You can also output the key/value pairs as a definition list, using php like:
<?php
$ru = <<<XMLDATA
<employee><id>FMCSC00015</id><year>2016</year><month>1</month><paymstr_salhdnm>BASIC PAY</paymstr_salhdnm><paymstr_amount>35600</paymstr_amount><paymstr_salhdnm>ASSOCIATION SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>240</paymstr_amount><paymstr_salhdnm>TELEPHONE ALLOWANCE</paymstr_salhdnm><paymstr_amount>800</paymstr_amount><paymstr_salhdnm>HOUSE RENT DEDUCTION</paymstr_salhdnm><paymstr_amount>2587.5</paymstr_amount><paymstr_salhdnm>MEDICAL ALLOWANCE</paymstr_salhdnm><paymstr_amount>700</paymstr_amount><paymstr_salhdnm>GAS BILL</paymstr_salhdnm><paymstr_amount>450</paymstr_amount><paymstr_salhdnm>DEARNESS ALLOWANCE</paymstr_salhdnm><paymstr_amount>6000</paymstr_amount><paymstr_salhdnm>LIFE INSURANCE PREMIUM (D)</paymstr_salhdnm><paymstr_amount>1718</paymstr_amount><paymstr_salhdnm>PF SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>3560</paymstr_amount><paymstr_salhdnm>PF ADVANCE LOAN</paymstr_salhdnm><paymstr_amount>2796</paymstr_amount><paymstr_salhdnm>BENEVOLENT FUND</paymstr_salhdnm><paymstr_amount>1780</paymstr_amount><paymstr_salhdnm>HEALTH INSURANCE PREMIUM</paymstr_salhdnm><paymstr_amount>150</paymstr_amount><paymstr_salhdnm>STUDENT WELFARE</paymstr_salhdnm><paymstr_amount>3</paymstr_amount><paymstr_salhdnm>MUNICIPAL TAX & SWEAPER CH.</paymstr_salhdnm><paymstr_amount>18</paymstr_amount><paymstr_salhdnm>STAIR CASE LIGHT CHARGES</paymstr_salhdnm><paymstr_amount>1.25</paymstr_amount><paymstr_salhdnm>CLUB</paymstr_salhdnm><paymstr_amount>207</paymstr_amount><paymstr_salhdnm>TEACHER'S FAMILY WELFARE ASSOC</paymstr_salhdnm><paymstr_amount>10</paymstr_amount><paymstr_salhdnm>MOTOR GARAGE</paymstr_salhdnm><paymstr_amount>500.5</paymstr_amount><paymstr_salhdnm>RESEARCH ALLOWANCE</paymstr_salhdnm><paymstr_amount>1500</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-3</paymstr_salhdnm><paymstr_amount>5728</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-4</paymstr_salhdnm><paymstr_amount>23490</paymstr_amount></employee>
XMLDATA;
$xml = simplexml_load_string($ru) or die("Error: Cannot create object");
// Collect and create headers
$headers = array('ID', 'YEAR', 'MONTH');
foreach ($xml->paymstr_salhdnm as $key=>$header) {
$headers[] = (string)$header;
}
echo '<dl>';
$hindex=0;
foreach ($xml->children() as $key=>$value){
if (strtolower($key) == 'paymstr_salhdnm' ) continue;
echo (isset($headers[$hindex])) ? PHP_EOL . "<dt>{$headers[$hindex]}</dt>" : PHP_EOL . '<dt></dt>';
echo "<dd>$value</dd>";
$hindex++;
}
echo '</dl>';
And the outcome being a list of "definition-terms" with the matching "definition-value", like so:
https://jsfiddle.net/9c2xaa27/

Preg_match_all how to correctly get all info?

Can't understand how correctly get all info from web, not just one row but all.
This script produces the output : Array Array Array Array and only one row.
<table id="rounded-corner" width=100%>
<tr>
<td>Nuotrauka</td>
<td>Pavadinimas</td>
<td>miestas</td>
<td>metai</td>
<td>kaina</td>
</tr>
<?
$url = "My Link";
$contents = file_get_contents($url);
preg_match_all("|<span class=\"ttitle2\">(.*?) </span>|U",$contents,$pavadinimas);
preg_match_all("|<span class=\"ttitle3\">(.*?)</span>|U",$contents,$miestas);
preg_match_all("|<span class=\"ttitle1\">(.*?)</span>|U",$contents,$metai);
preg_match_all("|<span class=\"ttitle1\" style='float: left;'>(.*?)<br />|U",$contents,$kaina);
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $contents, $img_link);
$output = "<tr><td><img src=$img_link></td><td>$pavadinimas</td><td>$miestas</td><td>$metai</td><td>$kaina</td></tr>";
print_r($output);
?>
</table>
Try this. It is extracting data as required.
<table style="border: 1px solid black;" id="rounded-corner" width=100%>
<tr>
<td style="border: 1px solid black;">Nuotrauka</td>
<td style="border: 1px solid black;">Pavadinimas</td>
<td style="border: 1px solid black;">miestas</td>
<td style="border: 1px solid black;">metai</td>
<td style="border: 1px solid black;">kaina</td>
</tr>
<?php
$url = "put_your_url";
$contents = file_get_contents($url);
preg_match_all('/<span class="ttitle2".*?>(.*?)<\/span>/',$contents,$pavadinimas);
preg_match_all('/<span class="ttitle3".*?>(.*?)<\/span>/',$contents,$miestas);
preg_match_all('/<span class="ttitle1".*?>(.*?)<\/span>/',$contents,$metai_kaina);
foreach($metai_kaina[0] as $key=>$metai_kaina_val){
if($key%2==0)
$metai[] = strip_tags($metai_kaina_val);
else
$kaina[] = strip_tags($metai_kaina_val);
}
preg_match_all('/<img .*?(?=src)src=\"([^\"]+)\"/si', $contents, $img_link);
for($i=0; $i<count($pavadinimas[0]); $i++){
echo '<tr>
<td style="border: 1px solid black;"><img src="'.$img_link[1][$i+2].'"></td>
<td style="border: 1px solid black;">'.$pavadinimas[0][$i].'</td>
<td style="border: 1px solid black;">'.$miestas[0][$i].'</td>
<td style="border: 1px solid black;">'.$metai[$i].'</td>
<td style="border: 1px solid black;">'.$kaina[$i].'</td>
</tr>';
}
?>
</table>
The $matches parameter of the function will always be an array - you need to process the variable before adding them to the output string.
http://php.net/manual/en/function.preg-match-all.php
As far as I'm aware you can't print_r a mixture of string and array.
If all matchings return the same number of results, which seems to be implied by your $output structure, iterate over one of them:
$output = '';
foreach ($pavadinimas[1] as $index => $match) {
$output .= '<tr><td><img src=' . $img_link[1][$index] . '></td><td>' . $match . '</td><td>' . $miestas[1][$index] . '</td><td>' . $metai[1][$index] . '</td><td>' . $kaina[1][$index] . '</td></tr>';
}
preg_match_all makes deep complex search. Maximum it'll be two dimentional array. Read docs
how to correctly get all info?
For this to find out you just dump out all the result info:
print_r($pavadinimas);
print_r($miestas);
...
but i need that all info put in table
Be little more inventive; if $pavadinimas is 2d array:
echo '<table>';
foreach($p in $pavadinimas){
echo '<tr>';
foreach($item in $p){
echo '<td>' , $item , '</td>' ;
}
echo '</tr>';
}
echo '</table>';
Note
To invert the 2d array order you just use flag (PREG_PATTERN_ORDER or PREG_SET_ORDER) in the preg_match_all (see flag section in docs). Thus you will have your table with other order.

How to show selected table row id in the address bar with ajax

I have this below script that load the selected table row details in a div in the same page with ajax.
$('.positiontitle-link').click(
function(){
var durl = $(this).attr('ref');
$('#details').load(durl)
}
)
This is my Table where it have two column. First column used to list the jobs from MySQL database and the second used to view the selected row full details.
<table class="table1" width="100%" cellpadding="0" cellspacing="0" style="height: 100%; z-index:1; top:0; position: absolute;">
<tr>
<td width="30%" style=" min-width:40px;padding:0; background-color:rgba(255, 255, 255, 0.9); height: 100%; bottom: 0; top:0;" valign="top">
<div style="margin: 0; padding: 0; overflow-x:hidden; height: 100%; position: inherit; bottom: 0; top:0;">
<?php
require "module/call.php";
?>
</div>
</td>
<td width="60%" style="min-width:40px;padding:0; background-color:rgba(255, 255, 255, 0.9); height: 100%; bottom: 0; top:0;" valign="top"><div id="details" style="overflow:auto; width:100%; border-left-width: thin; border-right-width: 0; border-top-width: thin; border-bottom-width: 0; height: 100%; bottom: 0; top:0;"></div></td>
<td width="20%" style="padding:0;"> </td>
</tr>
</table>
This is the imageof the table
This is the call.php codes
<?php
$result = mysqli_query($conn,"SELECT * FROM job where approved='1' ORDER BY `CreatedTime` DESC");
echo "<table id='maintable' class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Job Title</th>
<th position='fixed' width='5%'>Company Name</th>
<th width='5%'>Closing Date</th>
</tr>";
while($row = mysqli_fetch_array($result) )
{
if (strlen($row['positiontitle']) > 20) $row['positiontitle'] = substr($row['positiontitle'], 0, 60) . "...";
echo "<tr onclick='get_data(123)' ref='job.details.php?id=".$row['id']."' target='content' class='positiontitle-link'>";
echo "<td data-id='<?php echo $row['id']?>'><font style='text-shadow: none;'>" . $row['positiontitle'] . "</font></a></td>";
echo "<td data-id='<?php echo $row['id']?>'>" . $row['companyname'] . "</td>";
echo "<td data-id='<?php echo $row['id']?>'>" . $row['closingdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
and this is the details.php codes
<?php
$result = mysqli_query($conn,"SELECT * FROM job WHERE id = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");
$jobdetails = mysqli_fetch_assoc($result);
echo ''.$jobdetails['positiontitle'].'';?>
</title>
</br>
<div style="width:100%; margin-top:-20px;">
<!-------------------------------------------Start of Right Sidebar---------------------------------------------------------------->
<div style="float:left; font-size: 14px; width:20%;"class="ara-form">
<header style="padding-top: 25px; font-size: 14px; color:#666666; padding-left:7px; padding-right:7px;">
<?php
$result = mysqli_query($conn,"SELECT * FROM job WHERE id = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");
$jobdetails = mysqli_fetch_assoc($result);
echo '<strong>Job Title</strong><hr class="style-six"> '.$jobdetails['positiontitle'].'</p></br>';
echo '<strong>Company Name</strong><hr class="style-six"> '.$jobdetails['companyname'].'</p></br>';
echo '<strong>Location</strong><hr class="style-six"> '.$jobdetails['location'].'</p></br>';
echo '<strong>Closing Date</strong><hr class="style-six"> '.$jobdetails['closingdate'].'</p></br>';
echo '<strong>Number of Vacancy</strong><hr class="style-six"> '.$jobdetails['numberofvacancy'].'</p></br>';
echo '<strong>Job Category</strong><hr class="style-six"> '.$jobdetails['jobcategory'].'</p></br>';
echo '<strong>Duration</strong><hr class="style-six">'.$jobdetails['duration'].'</p></br>';
echo '<strong>Employment Type</strong><hr class="style-six"> '.$jobdetails['employmenttype'].'</p></br>';
echo '<strong>Salary</strong><hr class="style-six"> '.$jobdetails['salary'].'</p></br>';
echo '<strong>Timing</strong><hr class="style-six"> '.$jobdetails['timing'].'</p></br>';
echo '<strong>Nationality</strong><hr class="style-six"> '.$jobdetails['nationality'].'</p></br>';
echo '<strong>Gender</strong><hr class="style-six">'.$jobdetails['gender'].'</p></br>';
echo '<strong>Experience</strong><hr class="style-six">'.$jobdetails['experience'].'</p></br>';
echo '<strong>Education</strong><hr class="style-six"> '.$jobdetails['education'].'</p></br>';
echo '<strong>Gender</strong><hr class="style-six"> '.$jobdetails['gender'].'</p></br>';
echo '<strong>Gender</strong><hr class="style-six"> '.$jobdetails['gender'].'</p></br>';
?>
</header>
</div>
<!---------------------------------------------End of Right Sidebar------------------------------------------->
<div style="float:left; font-size: 14px; width:80%;" class="ara-form">
<fieldset style="font-size: 14px; color:#666666;">
<!-------------------------------------------------Start Time viewed Button------------------------------------------------>
<div style="width:100px; float:right; padding-left:2px; padding-top: 10px;">
<?php
include "module/button/job.views.php";
?>
</div>
<!---------------------------------------------------End Time viewed Button------------------------------------------------->
<!-----------------------------------------------Start Main Content----------------------------------------------->
<?php
echo '<p><strong>Company Background</strong><hr class="style-six"> '.$jobdetails['background'].'</p></br>';
echo '<p><strong>Job Summary</strong><hr class="style-six"> '.$jobdetails['summary'].'</p></br>';
echo '<p><strong>Job Duties and Responsibilities</strong><hr class="style-six"> '.$jobdetails['duty'].'</p></br>';
echo '<p><strong>Qualification</strong><hr class="style-six"> '.$jobdetails['qualification'].'</p></br>';
echo '<p><strong>Skills</strong><hr class="style-six"></br> '.$jobdetails['skill'].'</p></br>';
echo '<p><strong>Submission Guideline</strong><hr class="style-six"> '.$jobdetails['submission'].'</p></br>';
echo '<p><strong>Words to search this job</strong><hr class="style-six"> '.$jobdetails['search'].'</p></br>';
?>
<!-------------------------------------------------End Main Content----------------------------------------------->
</fieldset></div>
My Question is
1- I want to show the selected row id in the address bar
2- I want to show unclicked.php in the details div when no row selected
If you want to change the content of the address bar without refreshing the page, you'll either need to use the HTML5 History API or a hashbang URL.
HTML5 History API: This will allow you to change the content of the address bar to whatever you like, but the browser support is limited. An example is as follows:
history.pushState(null, "", "/test");
This will change the page URL to yoursite.com/test.
You can use this to add a row-related extension to the URL, eg. yoursite.com/table/row/20.
See here for a more in-depth explanation of how the API works.
Hashbang URL: This is a less clean solution, but is somewhat simpler and will work on all browsers. Your URL will look like yoursite.com/table#row20. (If you already have a # in the page URL, this will not work). Just call window.location = "#row20" to add #row20 to the current page URL.
If you are going to change the URL when a new row is selected, then you should also check the URL on page load and preselect a row if required. Eg. requesting yoursite.com/table#row20 would return the page with row 20 already selected.
$('#details').load('path/to/unclicked.php') will load the file's contents into the div. If you want this to occur on page load, just wrap it in a call to $(document).ready(). Simply make the AJAX request again if the user is able to deselect all rows.

Border around specific table row

I am trying to create a border around the top row ONLY! Currently, a border will be displayed only around the outside of the table. But I also want a border around the first row. Can someone help me do this? I want the row with 'Team, Correct Picks, and Points' to have a border around it.
<body>
<?=$leaguename?>
<center><table cellspacing="0" style="width:400px; border:1px solid gray">
<?
echo "<tr border=\"1\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr style=\"border:1px solid gray\" bgcolor=\"gray\"><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
</table></center>
</body>
This is presentational, so mixing it into the PHP is a bad idea. Instead, use CSS:
tr:first-child td { border-top: 1px solid black; border-bottom: 1px solid black; }
tr:first-child td:first-child { border-left:1px solid black; }
tr:first-child td:last-child { border-right:1px solid black; }
IE7+8 support for the *-child selectors can be a bit buggy. If it's not working in those browsers, consider using JavaScript polyfills.
You may also want to put the headers inside <thead> and <th> elements to make the <tr> with data the first row in a <tbody> element.
e.g.
<table>
<thead>
<tr>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>

PHP: working with tr and td with while()

<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while ($pF = mysql_fetch_array($stringVisits)) {
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if (checkStatus($BuID) == 1) {
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if (!empty($getByProfile["photo_thumb"])) {
echo $getByProfile["photo_thumb"];
} else {
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php } ?>
</tr>
</table>
This is what i have right now it displays the visiter´s profileimage. Now i would like to have their name under the image too.. but then i need to create another <tr> after this one, and add their name in each <td>. But how can i do that, if i have this while? Should i run another while, to get the names or can i do something smart with this?
Why not just stick the name in the same cell as the image? After you close the image tag, just echo $getByProfile["username"], or whatever?
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while($pF = mysql_fetch_array($stringVisits)){
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'><br/>";
?>
</a>
<br/><?php echo $getByProfile['username']; ?>
</td>
<?php } ?>
</tr>
</table>
Here's a way to do it with minimal changes to your existing code:
<?php
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
$names = array(); // ADDITION #1
while($pF = mysql_fetch_array($stringVisits)){
$names[] = // ADDITION #2 - ADD NEW USER NAME HERE;
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php }
// ADDITION #3:
echo "</tr>
<tr>";
foreach ($names as $username)
echo "<td class=\"username\"><p>$username</p></td>";
?>
</tr>
</table>
Also note that the way you had your code, the </tr> was inside the while loop.
this might not look nice but it should work why do you want 2 loops?
<?php
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >";
while($pF = mysql_fetch_array($stringVisits)){
echo "
<tr>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<a href='profil.php?id=".$BuID."'> <img /> </a>
</td>
</tr>
<tr><td> " . $getByProfile['username'] . " </td></tr>";
}
echo "</table>";
?>

Categories