If you believe this is a duplicate please let me now as I'm not totally sure what to search for to check, (I will remove if duplicate)
I have a list of of numbers in a mySql field called sess_times in a table called test_sess, the numbers for the first row are:
25, 38, 40, 50
is it possible to split these into php variable e.g:
$no1 = 25
$no2 = 38
$no3 = 40
$no4 = 50
I'm trying to put the individual numbers in a table, something like below:
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td> 1 </td>
<td><?php echo $no1; ?></td>
</tr>
<tr>
<td> 2 </td>
<td><?php echo $no2; ?></td>
</tr>
<tr>
<td> 3 </td>
<td><?php echo $no3; ?></td>
</tr>
<tr>
<td> 4 </td>
<td><?php echo $no4; ?></td>
</tr>
</table>
Any help with this would be great so thanks in advance for any answers.
HERE'S THE SOLUTION I USED, A COMBINATION OF 2 ANSWERS BELOW:
$data = '25,38,40,50';
$string = explode(",", $data);
$number = 0;
echo '<table cellspacing="0" cellpadding="0" border="0">';
for($i=0; $i<count($string); $i++)
{
$number = $number +1;
echo '
<tr>
<td>' . $number . '</td>
<td>' . $string[$i] . '</td>
</tr>
';
}
You can use PHP's explode function.
When you output everything, save it into a variable.
Then, $string = explode(",", $string); will produce this:
Array ( [0] => 28[1] => 38 [2] => 40 [3] => 50 )
To output this, you can then use $string[number] for each value.
You can explode them
$string="25,38,40,50";
$numbers=explode(",",$string);
print_r($numbers); // You can use them for printing like echo $numbers[0]; or 1 or 2 or 3
And if you don't want the result in an array and still want those 4 variable names you can do this
$string="25,38,40,50";
list($no1,$no2,$no3,$no4)=explode(",",$string);
This would give you the table output and it doesn't require the total amount to be known.
$data = '25,38,40,50';
$arr = explode(',', $data);
echo '<table cellspacing="0" cellpadding="0" border="0">';
for($i=0; $i<count($arr); $i++)
{
echo '
<tr>
<td>' . $i+1 . '</td>
<td>' . $arr[$i] . '</td>
</tr>
';
}
echo '</table>';
Use explode();
ex:
$numbers='25,38,40,50';
$numbers_array= explode(",", $no);
foreach($numbers_array as $key => $number)
{
$var_name='no'.(++$key);
$$var_name=$number;
}
echo $no1.'<br>;
echo $no2.'<br>;
echo $no3.'<br>;
echo $no4.'<br>;
Related
I am having a problem to convert my $quantity_total which is as example (113) from 3 different products.
I want it to be in a table like below.
I have been trying to use chunk_split and explode but if i was able to succeed in that. I wouldn't be able to make it dynamic.
<table>
<tr>
<th>Quantity</th>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
$total=0;
$item_count=0;
$arr = array();
$quantity_all = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$quantity_all .=$quantity;
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4></div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;
$_SESSION['total'] = $total;
$_SESSION['item-count'] = $item_count;
}
$_SESSION['quantity-all'] = $quantity_all;
Is this possible? And i need it to be dynamic. So if it were 10 different quantities. It would make 10 table rows.
I hope someone can help me, would really appreciate it a lot! It's the last thing to finish my e-commerce webshop.
As mentioned in my comment, I don't think this is good solution... but the function you're looking for is str_split. https://stackoverflow.com/a/9814389/296555
http://sandbox.onlinephpfunctions.com/code/0bbee53cafafc0d5e8954e07d0abc2c86c6c89a8
<?php
$rows = '156165165489465131';
echo '<table>';
echo '<tr><th>Quantity</th></tr>';
foreach (str_split($rows) as $row) {
echo "<tr><td>$row</td></tr>";
}
echo '</table>';
I dont know if this Is what you want, but you can try something like:
<table>
<tr>
<td>Quantity</td>
</tr>
<?php
$characters = str_split( (string) $quantity_total);
foreach($characters as $char){
echo "
<tr>
<td> $char </td>
</tr>
";
}
?>
</table>
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/>";
for ($i=0; $i<=$lines; $i++)
{
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
This is my code, it read's from a text file and explode in table, but I have a little problem here cause this one needs to be link.
<td width="20%">'.$part[2].'</td>
.$part[2]. is just a word from file but it has query like www.somesite.com/?q= There at the end I need to have that
word from file
that kind of code did not work for me
<td width="20%"> <a herf='www.somesite.com/?q=''.$part[2].'> '.$part[2].' </a> </td>
I realy need some help with this...
<?php
//first, get the file...
$file = file('req.txt');
//now count the lines ..
$lines = count($file);
//start the table here..
echo'<table border="2" width="100%">';
echo'<tr>
<td width="20%">Naslov</td>
<td width="20%">Vrsta</td>
<td width="20%">IP</td>
<td width="20%">Dodano (DD.MM.YY - HH.MM)</td>
<td width="20%">Status</td>
</tr>';
//start the loop to get all lines in the table..
for ($i=0; $i<=$lines; $i++) {
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
//close the table so HTML wont suffer :P
echo'</table>';
?>
This should produce this but ip column need to be link...
I think vprintf() is your friend.
<?php
$fmt = '<tr>
<td>%1$s</td>
<td>%2$s</td>
<td>%3$s</td>
<td>%4$s</td>
<td>%5%s</td>
</tr>';
for ($i=0; $i<=$lines; $i++)
{
// get each line and explode it..
$part = explode('|', $file[$i]);
// now start printing ..
vprintf($fmt, $part);
}
And put the width="20%" into your CSS.
I solve it alone with changing some values in input script "file writer"
$savestring = $title . "|" . $genre . "|<a href=http://www.example.com/ip?ip=" . $ip . ">" . $ip . "|" . $date . "|Za Naložit \n";
it works now ty anyway :)
Have array named for example $data_debit_turnover
Array
(
[0] => Array
(
[VatReturnRowNumberForDebitTurnover] => 63
[Total] => 0.00
)
[1] => Array
(
[VatReturnRowNumberForDebitTurnover] => 64
[Total] => 44.28
)
)
Have HTML that need to look like this
<table><tr>
<td><strong>63</strong></td>
<td>0.00</td>
</tr><tr>
<td><strong>64</strong></td>
<td>44.28</td>
</tr></table>
At first tried with php foreach, but in such case instead of one table get multiple tables [0], [1] etc.
Then tried
<td><strong>64</strong></td>
<td><?php
if( $data_debit_turnover[1][VatReturnRowNumberForDebitTurnover] == '64'){
echo $data_debit_turnover[1][Total]. ' Total<br>';
}?>
</td>
but problem is with [1] etc. I do not know number of []; may be [1] and may be [30].
Tried something like this
$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}
<td><strong>64</strong></td>
<td><?php
if (in_array('64', $resultVatReturnRowNumberForDebitTurnover)) {
echo $resultTotal[1];
}?>
</td>
The same problem [1]. How to echo corresponding (index) value from the another array.
For example if 64 is the second value in array $resultVatReturnRowNumberForDebitTurnover then need to echo the second value from array $resultTotal.
Possibly there is some other way.
Please advice.
Showing what I did with foreach
<?php
foreach($data_debit_turnover as $i => $result){
?>
<table>
<tr>
<td><strong>63</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '63') {
echo $result[Total];
} ?>
</td>
</tr>
<tr>
<td><strong>64</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '64') {
echo $result[Total];
} ?>
</td>
</tr>
</table>
<?php
}
?>
Update Thanks to #user2340218 advice get some solution. For each <td> must use foreach. Possibly there is some better solution.
<table><tr>
<td><strong>64</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '64') {echo $vatReturn['Total'];}}?></td>
</tr><tr>
<td><strong>67</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '67') {echo $vatReturn['Total'];}}?></td>
</tr></table>
Solution Finally used solution #Daniel P advised.
$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}
$VatReturnRowNumberForDebitTurnoverModified = array_combine($resultVatReturnRowNumberForDebitTurnover, $resultTotal);
<table>
<tr>
<td><strong>62</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[62]; ?>
</td>
</tr>
<tr>
<td><strong>63</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[63]; ?>
</td>
</tr>
</table>
Actually have to accept #Daniel P answer:) But as understand can not accept 2 answers
I am guessing that VatReturnRowNumberForDebitTurnover numbers are unique so your array should look like this :
Array
(
[63] => 0.00
[64] => 44.28
)
The array index is your VatReturnRowNumberForDebitTurnover and the value is your total.
To test if a VatReturnRowNumberForDebitTurnover has a value your simply use isset()
if (isset($array[63])) {
echo $array[63]
}
Building the table :
<table>
<?php foreach($data_debit_turnover as $i => $total){ ?>
<tr>
<td><strong><?php echo $i; ?></strong></td>
<td><?php echo $total; ?></td>
</tr>
<?php } ?>
</table>
Do you mean this?
<table>
<?php foreach($data_debit_turnover as $vatReturn){ ?>
<tr>
<td><strong><?php print $vatReturn['VatReturnRowNumberForDebitTurnover'] ?></strong></td>
<td><?php print $vatReturn['total'] ?></td>
</tr>
<?php } ?>
</table>
Use foreach like this:
<table>
<?php
foreach ($Array as $item)
{
echo '<tr>';
echo '<td><strong>' . $item['VatReturnRowNumberForDebitTurnover'] . '</strong></td>';
echo '<td>' . $item['Total'] . '</td>';
echo '</tr>'
}
?>
</table>
How can I remove the last element in a do-while generated table?
In my case the last tr/td where div.dividing_line is stored.
The code:
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
++$i;
} while ($i < $ArrayLength+1);
For example: If I have an array with 6 items, normally the do-while will do the job, so finally there will be 6 tr's with data and 6 tr's with the dividing_line.
What I need is 6 tr's of data and 5 tr's of dividing_line. Is that possible?
Try this-
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i != $ArrayLength) {
echo '<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
}
++$i;
} while ($i < $ArrayLength+1);
Use an extra if Statement to check whether you are at the last element:
if (%i < $ArrayLength) { echo '<tr>...dividing_line</tr>'; }
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i < $ArrayLength)
{
echo '
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>';
}
++$i;
} while ($i < $ArrayLength+1);
I think your approach just needs an additional step.
It could be something like this:
$data = null;
foreach ($rows as $row) {
$data[] = "<tr><td valign=\"middle\">Data_Position</td><td valign=\"middle\">Data_Item</td><td valign=\"middle\">Data_Pieces</td><td valign=\"middle\">Data_Price</td></tr>";
}
print implode("<tr><td colspan=\"4\"><div class=\"dividing_line\"></div></td></tr>", $data);
This way, you could accomplish what you want without any more logic. Of course, it can be changed or re-design, but I think this way will provide you with a simple yet elegan solution to your problem.
Hope it helps :P