Preg_match_all how to correctly get all info? - php

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.

Related

How do i put a <td> in my <table> with 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.

Create a Table With Merged TH Cells

I am creating an Excel download code, in which I am merging the TH cells and looping through TR to put data under specific merged TH.
But all of data is reflecting on first TH row. Below is code:
$K=0;
while($ROW = $RESULT->fetch_assoc() )
{
if($ROW['type'] == $_GET['separate_id'])
{
$DATE[] = $ROW['end_time'];
$meta_values[] = $ROW['meta_values'];
$body = '
<table style="border:1px solid #000; border-collapse:collapse;">
<thead style="border:1px solid #000; padding:8px;width:150px; height:25px;font-size:15px">
';
foreach($DATE as $DATES)
{
$DATESVIEW = explode('T',$DATES);
$body .=' <th colspan="2">'.$DATESVIEW[0].'</th> ';
}
$body .= '
</thead>
';
foreach($meta_values as $VALS)
{
$META_VALS = explode(' | ',$VALS);
foreach($META_VALS as $VALUEs)
{
$REs = explode(' - ', $VALUEs);
$body .= '
<tbody>
<tr style="border:1px solid #000; padding:8px;width:150px; height:25px;font- size:15px">
<td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[0].'</td>
<td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[1].'</td>
</tr>
</tbody>
';
}
}
}
$K++;
}
$body .= '
</table>
';

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/

sending checkbox data in table through mail

I am trying to send selected check-box data through mail in the tables structure but getting below error:
implode(): Invalid arguments passed
static function mcontent($chkbox){
$sql = "SELECT station,reach,language,rate FROM `radio_city` where chkid in (".implode(',', $chkbox).")";
return DB::select($sql);
}
Please help.
Thanks
Jyoti
View1: this is used while sending data through mail
<input type="hidden" name="checkbox" id="checkbox" >
<input type="hidden" name="hiddamount" id="hiddamount">
<input type="email" name="email" id="email">
<input type="submit" id="submit" class="button" style="box-shadow: 5px 5px 3px #888888;" value="Email Plan" onsubmit="return validateForm();checkbox();"/>
function checkbox(){
if (document.myForm.checkbox.checked){
document.getElementById("error").innerHTML = "";
return true;
}
else {
document.getElementById("error").innerHTML = "Select your Plans";
}
}
View2: this is used while selecting checkboxes
<td onchange="Process(this.options[this.selectedIndex].value)" class="chart_row" align="center" >
<input type="checkbox" name="checkbox[<?php echo $i; ?>]" id="checkbox" value="<?php echo $chkid; ?>" data-price="<?php echo $total; ?>" onChange="updateTotal(this);">
</td>
<td id="station" class="chart_row" align="center"><?= $result->station ?></td>
<td id="reach" class="chart_row" align="center"><?= $result->reach ?></td>
<td id="language" class="chart_row" align="center"><?= $result->language ?></td>
<td id="rate" class="chart_row" align="center">Rs <?= $result->rate ?>/-</td>
<td id="duration" class="chart_row" align="center"><?= $duration ?></td>
<td id="frequency" class="chart_row" align="center"><?= $frequency ?></td>
<td id="hours" class="chart_row" align="center"><?= $hours ?></td>
<?php $totals += ($result->rate) * $duration * $frequency * $hours * $days/10; ?></div>
</tr>
Controller:
function mail() {
$input = Input::get();
if (isset($_GET['checkbox'])){
$city = $_GET['city'];
$duration = $_GET['duration'];
$frequency = $_GET['frequency'];
$hours = $_GET['hours'];
$days = $_GET['days'];
$total = $_GET['hiddamount'];
$chkbox = $_GET['checkbox'];
$mailrslt = Radio::mcontent($chkbox);
$cityname = Radio::getall($input);
foreach($cityname as $cities){
//modify table ans add css here if you want.
$html_table ="<table><caption>City: ".$cities->city."</caption><thead style='color:A0A0A0'><th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Station</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Reach</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Language</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Rate</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Jingle Lenght</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Frequency</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Hours/Day</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Days/Week</th></thead><tbody style='color: #000000;'>";}
//whole query result is here
foreach($mailrslt as $key=>$row) {
$html_table .= "<tr>";
//Iterate your data for table data.
foreach($row as $key2=>$row2){
$html_table .= "<td width='200' align='center' class='tabledata'>" . $row2 . "</td>";
}
//Database data ends here
$html_table .= "<td width='200' align='center' class='tabledata'>" . $duration . "</td>";
$html_table .= "<td width='200' align='center' class='tabledata'>" . $frequency . "</td>";
$html_table .= "<td width='200' align='center' class='tabledata'>" . $hours . "</td>";
$html_table .= "<td width='200' align='center' class='tabledata'>" . $days . "</td>";
} //table rows ends here for displaying data
$html_table .= "</tr></tbody></table>";
//contact details and total calculation done here
$html_table .= "<p>The total negotiable cost for your activity is <b>Rs ".$total."/-</b></p><div float='right'></p><u><b>For Best Rates</b></u></br> Call: Samir-+919686595443</br>Email: samir#themediaant.com</p></div>";
$to = $input['email'];//user email id from form.
$from = 'help#themediaant.com';//from email ID
$subject = 'Self Help Radio Planner';//subject
$totals = 0;
$message = $html_table;//assigning html_table variable to message.
$contact_enquiry = new ContactEnquiry;
$contact_enquiry->email = $from;
$contact_enquiry->message = $message;
$contact_enquiry->save();
//final mail function goes here
$mail = Mail::send('emails.media_option_assistance', ['msg' => $message], function ($msg) use ($from, $to, $subject) {
$msg->from($from, 'The Media Ant');
$msg->to($to)->subject($subject);//->cc('servicing#themediaant.com');
});
//after successful mail redirect user to same page.
return Redirect::to('/radio-plan/radioplan')->with('message','<b>Congratulations! You have succesfully sent the email');
}
}
Model:
static function mcontent($chkbox){
$sql = "SELECT station,reach,language,rate FROM `radio_city` where chkid in (".implode(',', $chkbox).")";
return DB::select($sql);
}
Your problem is here
$chkbox = $_GET['checkbox'];
$mailrslt = Radio::mcontent($chkbox);
implode requires the second parameter (or the first if only one is supplied) to be an array. You are providing mcontent with a string that you get from the input. So when you reach implode inside mcontent, you are trying to convert a string to a string.
So you must either create an array with one or more variables to be passed to your function, or remove the implode from the function.
I would try to help more but honestly I don't understand much from your code and it would be a guessing game.

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>

Categories