Hello There I'm having a problem creating an array
<?php
//I'm actually grabbing the list from MySQl
//$list = '"02","03"';
$friends_list_array = array($list);
echo $friends_list_array[0];
?>
This is the Code !
But It Doesn't Work
Expected Result : 02
Output what i got from above code : "02","03"
Someone help please ?
Use php explode() function:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo $friends_list_array[0];
?>
Output:-https://eval.in/839531
If you want output strictly 02:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo trim($friends_list_array[0], '"');
?>
Output:-https://eval.in/839537
You can try also this way:-
<?php
$friends_list_array = array(
"02",
"03"
);
echo $friends_list_array[0];
Related
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
That is my Code :
<?php
$url = 'http://www.ebay.de/itm/321773181887';
$aufrufe_content = file_get_contents($url);
$aufrufe1 = explode( '<span id="vi-time-wrapperSection">' , $aufrufe_content );
$aufrufe2 = explode("</span></span>" , $aufrufe1[1] );
echo $aufrufe2[0];
?>
How i can remove the word "Restzeit: " ?
I read on other sites I can solve my problem with "str_replace" but I dont know how I can use that in my code.
It is as simple as that :
<?php
// ... the first 4 lines
$aufrufe2 = explode("</span></span>" , $aufrufe1[1]);
$temp = str_replace('Restzeit: ','',$aufrufe2[0]);
echo $temp;
?>
I'm trying to integrate a tracking pixel into the magento success page. For testing I build the following code and implemented it into line > 45 in success.phtml file within the template folder. Actually the variables are all empty. What's wrong?
<?php
<?php
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$skus = array();
$qtys = array();
$amounts = array();
foreach ($order->getAllItems() as $item){
$skus[$item->getProductId()] = $item->getSku();
$names[$item->getProductId()] = $item->getName();
$qtys[$item->getProductId()] = $item->getQtyOrdered() * 1;
$amounts[$item->getProductId()] = $item->getRowTotal() * 100;//or $item->getPrice()*$item->getQtyOrdered();//but this will ignore any applied coupons
}
$skuspipe = implode("|", $skus);
$namespipe = implode("|", $names);
$qtyspipe = implode("|", $qtys);
$amountspipe = implode("|", $amounts);
<!--
OrderID: <?php echo $orderID; ?>
skus: <?php print_r($skus); ?>
names: <?php print_r($names); ?>
qtys: <?php print_r($qtys); ?>
amounts: <?php print_r($amounts); ?>
skupipe: <?php echo $skupipe; ?>
namespipe: <?php echo $namespipe; ?>
qtyspipe: <?php echo $qtyspipe; ?>
amountspipe: <?php echo $amountspipe; ?>
-->
Thank you!
In Collections, Magento often only loads kind of a stub of data for each item.
You could load the whole objects by using
$item->load( $item->getId() );
on each iteration.
Also, try to debug output the collection first to see if there are any items found.
<?php
$json = file_get_contents('http://tiny.cc/ttrhelp');
$obj = json_decode($json);
$example = $obj->rooms->displayName;
?>
Name: <?php echo $example; ?>
Trying to show the value for 'displayName' but its not showing
Untested code:
<?php
$json = file_get_contents('http://pub.tapulous.com/tapplications/coresocial/v1/chat/api/index.php?method=room_list');
$obj = json_decode($json);
foreach($obj->rooms as $room){
$example = $room->displayName;
echo $example;
}
?>
You probably want $obj->rooms[0]->displayName.
CodePad.
echo $obj->rooms[2]->displayName;
Try
echo $obj->rooms[0]->displayName;
<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
if($child_cats) :
echo '{ ';
foreach($child_cats as $cat) {
echo $sep . $cat->cat_name;
$sep = ', ';
}
echo ' }';
endif;
?>
The above code outputs a number of categorys in this format:
A Cut Above,A20Labs,AMCH,
how would I add ' ' around each of the elements for output like this?
'A Cut Above','A20Labs','AMCH',
2nd question, how would I code it so that that the output goes into this array code like this?
<?php $type_array = array('A Cut Above','A20Labs','AMCH',)?>
Thanks so much!
Azeem
For your first question, change echo $sep . $cat->cat_name; to echo $sep . '\''.$cat->cat_name.'\'';
This will change it to output the name with single quotes around them.
To return an array instead, try this:
<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
$type_array = array();
if($child_cats) :
foreach($child_cats as $cat) {
$type_array[] = $cat->cat_name;
}
endif;
?>
This will place the names into a new array instead of echoing them.
You can get the array you're wanting with a lot less work:
<?php
$child_cats = get_categories(array(
'child_of' => $parent_cat,
'fields' => 'names'
));
?>