<table>
<tr>
<th>Products</th>
</tr>
<td>
<?
foreach ($invoice->cart->items as $id => $item) {
?>
<?=$item->getName()?>
<?
}
?>
</td>
</table>
If I have 4 Products then I want to separate with coma ?
For example if I have 4 Products:-
Rubber Ducky™ Gold fish & Tank David Byrne and My Fair Lady - MP3 file download (0 MB)
I want the output with coma like this
Rubber Ducky™, Gold fish & Tank, David Byrne, My Fair Lady MP3 file download (0 MB)
how it is possible with PHP?
<?php
$first = true;
foreach ($invoice->cart->items as $id => $item) {
if ($first) {
$first = false;
} else {
echo ', ';
}
echo $item->getName();
}
?>
This will print a ', ' between item names.
Alternatively, you could also do that:
<?=implode(', ', array_map(function($item) { return $item->getName(); }, $invoice->cart->items))?>
Use implode function. Something like
$array = $invoice->cart->items;
echo implode(',',$array);
$i = 0;
foreach( $invoice->cart->items as $item ) {
echo ($i?', ':'').$item->getName();
++$i;
}
Related
so my words is below, separated by space. my code is 1st line is grabbed from text file & whole line is echoed, instead i need each-word as ordered list.
• bombay new_delhi chennai culcutta
<?php
$z = file('q1.txt'); echo $z[1]; echo (mt_rand(1,9));
$st = array($z[1]);
foreach($st as $ls) {echo " <li>$ls</li>\n";}
i need like this
• bombay
• new_delhi
• chennai
• culcutta
Before the foreach()
echo '<ol>';
//foreach loop
echo '</ol>';
<?php
$z = file('q1.txt'); echo $z[1]; echo (mt_rand(1,9));
$c= explode(" ", $z[1]);
echo '<ol>';
foreach($c as $w) {echo " <li>$w</li>\n";}
echo '</ol>';
$var = file('q1.txt');
$abc = explode(' ', $var);
echo '<ul style=" list-style-type: circle;">';
foreach($abc as $value){
echo " <li>$value</li>";
}
echo '</ul>';
I have the following PHP script that extracts the last 5 rows from file X.
<?php
$f=file("x.txt");
$last=array_slice($f, -5);
echo implode("<br>",$last);
?>
The output is the following:
John
Christmas
George
Luck
Sun
Question: How can I make the output to be clickable links? Example of output I want:
John
Christmas
George
Luck
Sun
I tried something like:
echo implode("<br> <a href='http://google.com/q?' . $last . ''>");
But it didn't work at all... any solutions? Thanks!
Instead of implode(), you can just loop thru $last then echo desired html line
foreach ($last as $value) {
echo "<a href='http://www.google.com?q=$value'>$value</a><br>";
}
Try this :-
<?php
$names = array('John','Christmas','George','Luck','Sun');
foreach($names as $name) {
echo "<br>$name";
}
?>
Happy Coding :-)
I think no need to use implode function.
I have modified your code as below. Hope this will help.
<?php
$f=file("x.txt");
$last=array_slice($f, -5);
if(!empty($last)){
foreach ($last as $value) {
?>
<a href='http://www.google.com?q=<?php echo $value;?>'><?php echo $value;?></a><br>
<?php
}
}
?>
<?php
$f = array("Volvo", "BMW", "Toyota"); //$f=file("x.txt");
$last=array_slice($f, -5);
echo implode("<br>",$last);
foreach ($last as $value) {
echo "<a href='http://www.google.com?q=$value'>$value</a> , ";
}
?>
You can use array_map and implode if you don't want to loop.
$last = array('John','Christmas','George','Luck','Sun');
function addlink($last)
{
return('' . $last .'');
}
$last = array_map("addlink",$last);
echo implode("\n", $last);
https://3v4l.org/l3nME
Here is my code ,output and doubt ....
<?php
$csvData = file_get_contents("http://feed2.abc.com/PriceWs/PriceSnap.asmx/PriceUpdate?item=GC-APR16,SI-MAY16,PA-JUN16,PL-APR16,CL-APR16,BRCL-MAY16,NG-APR16,HO-APR16,HG-MAY16,CC-MAY16,KC-MAY16,CN-MAY16,CT-MAY16,WH-MAY16,SB-MAY16,SY-MAY16,BO-MAY16,&col=last,bid,ask,high,low");
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
$array[] = str_getcsv($line);
}
foreach($array as $k=>$v){
?>
<table border="1">
<tr>
<td> <?php echo $v[0]; ?><td>
<td> <?php echo $v[1]; ?><td>
<td> <?php echo $v[2]; ?><td>
</tr>`enter code here`
<?php
}
?>
OUTPUT WILL BE LIKE THIS:
GC-APR16 1234.40 1233.30
SI-MAY16 15.180 15.180
PA-JUN16 538.10 539.10
PL-APR16 954.50 955.60
HG-MAY16 2.0750 2.0745
CC-MAY16 2851.0 2849.0
KC-MAY16 119.90 119.90
CN-MAY16 361.75 361.50
CT-MAY16 59.44 59.42
WH-MAY16 460.25 460.00
SB-MAY16 14.33 14.33
SY-MAY16 905.00 905.00
BO-MAY16 33.87 33.86
HERE IS MY DOUBT:
INSTEAD OF GC-APR16 need to display "GOLD", SI-MAY16 need to display "SILVER"
PLEASE HELP....
MANY THANKS
$v[0] = GC-APR16 1234.40 1233.30';
// get first two chars
$two_chars = substr($v[0]);
// "multi if-else"
switch($two_chars) {
case 'GC': $new_tx = 'GOLD'; break;
...
default: ; //show error
}
echo $new_tx;
I have a list of names and each name consist of 2-4 words: name, (if exist) middle name(s), surname.
These are the names:
Ali Yilmaz
Taha Ugur Unal
Omer Ibrahim Tahsin Son
Recai Sahin
etc.
I want to print this names as this: Surname, name middle name(s) (if exist)
The names above will be:
Ali Yilmaz -> Yilmaz, Ali
Taha Ugur Unal -> Unal, Taha Ugur
Omer Ibrahim Tahsin Son -> Son, Omer Ibrahim Tahsin
Recai Sahin -> Sahin, Recai
etc.
If I had only one name I can use that code:
<?php $exp1 = explode(" ", $author1); ?>
<?php if (count($exp1) == 2) {?>
<?php print ($exp1[1] .', ' .$exp1[0]); ?>
<?php } elseif (count($exp1) == 3) {?>
<?php print ($exp1[2] .', ' .$exp1[0] .' ' .$exp1[1]); ?>
<?php } elseif (count($exp1) == 4) {?>
<?php print ($exp1[3] .', ' .$exp1[0] .' ' .$exp1[1] .' ' .$exp1[2]); ?>
<?php }?>
Each page can have different numbers of author and I thought I could use foreach to apply the above code for each author name but I couldn't do this.
I tried a piece of code such that:
<?php foreach $author as $obj): ?>
<?php $a = explode (" ", $obj) ?>
<?php endforeach ?>
...
But it gives error:
explode() expects parameter 2 to be string, array given.
How can I do this?
<?php
$names = array(
'Ali Yilmaz',
'Taha Ugur Unal',
'Omer Ibrahim Tahsin Son',
'Recai Sahin'
);
// First you should iterate through:
foreach ($names as $name) {
// and now, let make the job
// split by words
$parts = explode(' ', $name);
if (count($parts) == 1) {
echo "{$name}<br/>";
continue;
}
// get last word
$last = array_pop($parts);
// Print last one, comma, and rest of name
echo "{$last}, " . implode(' ', $parts) . "<br/>";
}
If you had a single string, you would
split all the words with explode().
take out the surname and store it.
join the rest of the name with implode().
See the example below:
<?php
$name = "Son of the Mask";
$words = explode(" ", $name); // split the string wherever there is a whitespace.
$surname = array_pop($words);
$restOfTheName = implode(" ", $words); // join the words with a whitespace in between them.
echo $surname . ", " . $restOfTheName;
?>
On the other hand, if you have a list of names in an array called, say, $namelist, you can use the foreach() loop to iterate through the list.
You would do something like:
<?php
foreach ($namelist as $name)
{
$words = explode(" ", $name);
$surname = array_pop($words);
$restOfTheName = implode(" ", $words);
echo $surname . ", " . $restOfTheName;
}
?>
Hope that helped :-)
EDIT:
And no, you need not use <?php and ?> on every line. This is only helpful, say when you want to use small snippets of PHP inside your HTML tags.
For example, let us say you want to display some information in an unordered list. Then you would do something like
<?php
$info1 = "Head over to Google.com";
$info2 = "Search before you post!";
?>
<ul>
<li><?php echo $info1; ?></li>
<li><?php echo $info2; ?></li>
</ul>
But, doing this for a script that contains only PHP code doesn't make sense. Moreover, it renders your code difficult to read, and eats up a lot of your valuable time.
you can try this
<?php
$newNameArray=array();
$namesArray = array(
'Ali Yilmaz',
'Taha Ugur Unal',
'Omer Ibrahim Tahsin Son',
'Recai Sahin'
);
foreach ($namesArray as $name) {
$partsOfName = explode(" ", $name);
//last name
$lastName = array_pop($partsOfName);
// store the name in new array
$newNameArray[]=$lastName.", ".implode(" ", $partsOfName);
}
//show all names
var_dump($newNameArray);
?>
you can see this for details explode and array push
use this
foreach ($author as $obj) {
$nameArr = explode(' ',$obj);
if(count($nameArr) > 1) {
$lname = $nameArr[count($nameArr)-1];
array_pop($nameArr);
echo $lname.", ".implode(' ',$nameArr);
echo "<br />";
} else {
echo $obj;
echo "<br />";
}
}
I loop through my array to print the articles names:
<?php
if ($articles) {
foreach($articles as $article) {
echo $article->name.", ";
} // end foreach article
} // end if has articles
?>
This will obviously produce something like
Apple, Banana, Mango,
But I am looking for:
Apple, Banana, Mango
I tried some implode statement like this:
<?php
if ($articles) {
foreach($articles as $article) {
echo implode(", ", $article->name);
} // end foreach article
} // end if has articles
?>
or
<?php
if ($articles) {
echo implode(", ", $articles->article->name);
} // end if has articles
?>
None of these are working for me. How can do it right? Thanks for hints!
You can use foreach to add the article names to an array, then implode() that array of names.
<?php
if ($articles) {
$article_names = array();
foreach($articles as $article) {
$article_names[] = $article->name;
} // end foreach article
echo implode(', ', $article_names);
} // end if has articles
?>
it's much more easy to check for your first loop-iteration, wrte the comma before your text and leave this comma aout on the first iteration:
<?php
if ($articles) {
$firstiteration = true:
foreach($articles as $article) {
if(!$firstiteration){
echo ", ";
}
$firstiteration = false;
echo $article->name;
} // end foreach article
} // end if has articles
?>
another (more beautiful in my optionion) possibility would be to override the _toSting()-method of your article-class:
...
function __toString(){
return $this->name;
}
...
and simply echo implode(", ",$articles)
It is better way to do what you want:
<?php
$string = '';
if ($articles) {
foreach($articles as $article) {
$string .= $article->name.", ";
}
}
$string = substr($string, 0, -2);
echo $string;
?>
PHP has a lot of good array functions, and this screams for one of them.
$namesArray = array_map(function($x){return $x->name;}, $articles);
$string = implode(',' $namesArray);
OR
$first = true;
array_walk(function($x) use (&$first)
{
if(!$first) {echo ', ';} else{$first = false;}
echo $x->name;
}, $articles);
I really like the above response with the __toString function also, but I wanted to show these array functions because i think they are often underused in favor or unnecessary foreach loops.
You can remove last comma and space by using trim function. It is the easiest way..
<?php
if ($articles) {
foreach($articles as $article) {
echo trim(implode(", ", $article->name), ', ');
}
}
?>