Currently I have this code:
<?php
echo '<meta name="robots" content="noindex">';
$arr = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json"),true);
foreach($arr as $item) {
$label = $item['label'];
$cost = $item['cost'];
$id = $item['paper_item_id'];
$member = $item['is_member'];
if ($member == "1") {
$member = "Yes";
}else{
$member = "No";
}
echo "$label = $id = $member = $cost";
echo ",<br>";
}
?>
What this code does it that it adds a tag after every line. This is the snippet that it happens in:
echo "$label = $id = $member = $cost";
echo ",<br>";
What I want is at the last line for the line echo ",<br>"; to NOT be ran. How do I do this? Please help!
Try this:
$result = array();
foreach($arr as $item) {
...
$result[] = "$label = $id = $member = $cost";
}
echo implode('<br />', $result);
Idea is to store all rows in array and join items with BR tag after your loop finished working.
Try this one:
<?php
$str = '';
$str .= '<meta name="robots" content="noindex">';
$arr = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json"),true);
foreach($arr as $item) {
$label = $item['label'];
$cost = $item['cost'];
$id = $item['paper_item_id'];
$member = $item['is_member'];
if ($member == "1") {
$member = "Yes";
}else{
$member = "No";
}
$str .= "$label = $id = $member = $cost";
$str .= ",<br>";
}
echo substr($str, 0, -5);
?>
I like ioseb's explode solution, but here's another easy way:
foreach($arr as $item) {
$first = isset($first) && print ",<br>";
// ...
echo "$label = $id = $member = $cost";
}
This prints the line break on all but the first iteration - but before the text, just make sure $first isn't set before running it.
I would recommend storing all your items in a string rather than echoing as you go. then you can remove whatever trailing characters you want when you're done before you echo just once at the end with a simple regex like...
echo preg_replace('/\<br\/\>$/','', $myTotalOutputString);
A small rewrite of your code:
<?php
echo '<meta name="robots" content="noindex">';
$arr = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json"), true);
$outArray = array();
foreach($arr as $item) {
$label = $item['label'];
$cost = $item['cost'];
$id = $item['paper_item_id'];
$member = ($item['is_member'] == '1') ? "Yes" : "No";
$outArray[] = "{$label} = {$id} = {$member} = {$cost}";
}
echo implode(',<br>', $outArray);
?>
Quick one-liner using rtrim:
echo rtrim('test <br />', '<br />');
But I voted for the implode answer above.
Related
I'm want string out of the column data.
But it failed.
<?php
$conn = mysql_connect("localhost", "nantawat", "12345678") or die(mysql_error());
$select_db = mysql_select_db("my_db", $conn) or die(mysql_error());
$select_tbl = mysql_query("SELECT * FROM log", $conn);
while ($fetch = mysql_fetch_object($select_tbl)) {
$r = $fetch->data;
$i = explode(",", $r);
if (!isset($i[1])) {
for ($j = 0; $j <= 200; $j++) {
$i[$j] = null;
}
}
$name = $i[0];
$mama = $i[1];
$no = $i[2];
$a = $i[3];
$b = $i[4];
echo $name . "</br>";
echo $mama . $no . $a . $b . "</br>";
}
while ($data = mysql_fetch_object($select_tbl)) {
echo $data->data . "<br>";
}
?>
But i want output =
bus
car
bike
aabus
car
bike
aabus
car
bike
aabus
ddd
ee
And i not
Notice: Undefined offset: 3 in C:\xampp\htdocs\logs\New folder
(2)\explode.php on line 21
Notice: Undefined offset: 4 in C:\xampp\htdocs\logs\New folder
(2)\explode.php on line 22
Thank You.
You should just do what you want to do.
You want to connect to database then do it:
$conn = mysql_connect("localhost", "nantawat", "12345678") or die(mysql_error());
I suggest you to use mysqli library instead of mysql (mysql is deprecated in new php versions and totally removed in php7)
$conn = mysqli_connect("localhost", "nantawat", "12345678", "my_db") or die(mysql_error());
You want to query on log table, then do it:
$select_tbl = mysqli_query($conn, "SELECT * FROM log");
You want to fetch info from your result, then do it:
while ($row = mysqli_fetch_array($select_tbl)) {
echo $row['id_user'];
echo $row['id_doc'];
echo $row['date'];
echo $row['data'];
}
You want to explode data, then do it:
while ($row = mysqli_fetch_array($select_tbl)) {
echo $row['id_user'];
echo $row['id_doc'];
echo $row['date'];
$data = explode(',', $row['data']);
foreach ($data as $d) {
if ($d !== '') { // because before first comma or after last can be empty
echo $d . PHP_EOL;
}
}
}
If you want to save database result in variables:
If you are getting only one row of database, you can save them in variables directly:
$id_user = '';
$id_doc = '';
$date = '';
$data = array();
id ($row = mysqli_fetch_array($select_tbl)) {
$id_user = $row['id_user'];
$id_doc = $row['id_doc'];
$date = $row['date'];
$tempData = explode(',', $row['data']);
foreach ($tempData as $d) {
if ($d !== '') {
$data[] = $d;
}
}
}
And if you have multiple rows of database you need to save them all in a total array:
$array = array();
id ($row = mysqli_fetch_array($select_tbl)) {
$id_user = $row['id_user'];
$id_doc = $row['id_doc'];
$date = $row['date'];
$data = array();
$tempData = explode(',', $row['data']);
foreach ($tempData as $d) {
if ($d !== '') {
$data[] = $d;
}
}
$array[] = array(
'id_user' => $id_user,
'id_doc' => $id_doc,
'date' => $date,
'data' => data,
);
}
And finally use this to see what structure your final array has:
echo '<pre>';
pront_r($array);
echo '</pre>';
First off it is not wise to store comma seperated values in a single cell and you are using deprecated mysql_ functions. I think your solution can be found in using a foreach instead of the isset part:
while ($fetch = mysql_fetch_object($select_tbl)) {
$r = $fetch->data;
$i = explode(",", $r);
foreach ($i as $q){
echo $q . '<br/>';
}
}
If you still want to access your variables $name, $mama, $no and $ab, you can use isset for those specifically.
while ($fetch = mysql_fetch_object($select_tbl)) {
$r = $fetch->data;
$i = explode(",", $r);
if (isset($i[0])){
$name = $i[0];
echo $name . '<br>'; //only echo if it exists
}
if (isset($i[1])){
$mama= $i[1];
echo $mama. '<br>'; //only echo if it exists
}
//try it yourself for $no and $ab
}
Try:
while ($row = mysqli_fetch_array($select_tbl)) {
extract($row);
/* Using extract method can get the array key value as variable
Below variables are available
$id_user;
$id_doc;
$date;
$data; */
}
Hello can you please help that how i can get description by this loop ?
$timepicker = $_POST['timepicker'];
$discription = $_POST['description'];
foreach( $timepicker as $key => $n )
{
print "".$n." ".$discription[$key]."\n";
}
foreach( $discription as $value)
{
print $value."\n";
}
<?php
for ($i = 0, $i<= count($_POST['timepicker']); $i++){
echo $timep=$_POST['timepicker'][$i]."<br>";
echo $name = $_POST['fullname'][$i]."<br>"; // get name
echo $job = $_POST['description'][$i]."<br>"; // get jobtitle
}
?>
The correct Answer is :-
foreach( $discription as $value)
{
print $value."\n";
}
I have an array $result and I want to echo $image for the first item only. My code this is:
<?php
foreach ($result as $item)
{
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
}
?>
i want echo $image in first item and don't echo it Second, or third, etc item
$item = reset($array);
// do stuff with $item here
^ gets the first item of the array without removing it.
Or just break before the closing } in your code.
<?php
$first = true;
foreach ($result as $item)
{
$image = ($first) ? $item->image : '';
$id = $item->id;
$cat = $item->catid;
$first = false;
}
?>
if you want display only first item, you can use array_shift
$item = array_shift($result);
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
You can count your iteration and print only at first run
$i=o;
foreach ($result as $item)
{
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
if($i==0)
{
echo $image;
}
$i++;
}
$arr = array('foo', 'baz', 'baz');
reset($arr);
$first = current($arr);
echo $first->image; // outputs 'foo'
Try this:
<?php
foreach ($result as $item)
{
$image = $result[0]->image;
$id = $item->id;
$cat = $item->catid;
}
?>
I sorted a list alphabetically and data from database mysql.To read the data i am using foreach loop.Now,I want when first letter (a->b->c) will change a seperator will be add after 'A' character list and so on.
Code:
$result = mysqli_query($con,"SELECT * FROM pages order by name ASC");
foreach($result as $val){
echo $val['name']."<br>"; echo "<hr></hr>";
}
Thanks in advance
<?php
$first = '';
$result = mysqli_query($con,"SELECT * FROM pages order by name ASC");
foreach($result as $val){
$first = substr($val['name'], 0, 1);
if ($first != $prev){
echo '<h1>'.$first.'</h1>';
}
echo $val['name']."<br>";
$prev = $first;
}
Try this:
$result = mysqli_query($con,"SELECT * FROM pages order by name ASC");
$lastChar = "?"; //initialization
foreach($result as $val){
if($lastChar != $val['name'][0]){
echo $val['name'][0].'<BR>'; //separator
}
echo $val['name']."<br>"; echo "<hr></hr>";
$lastChar = $val['name'][0]; //update
}
Try this (untested code) :
$firstLetter = 'A';
foreach( $result as $val ) {
$ch = substr( $val['name'],1,1);
if( $ch != $firstLetter ) {
echo "<hr>";
$firstLetter = $ch;
}
echo $val['name']."<br>";
}
$char = 'a';
foreach($result as $val) {
$newChar = substr($val['name'],0,1);
if ($newChar != $char) {
$char = $newChar;
echo "seperator for new character";
}
//do your other stuff
}
Is this what you were wanting to do?
I have text file as below:
text.txt
lebuzzdesbonsplans.com;hotmail.com;26608;828;3.11
lebuzzdesbonsplans.com;hotmail.fr;24798;876;3.53
friendcorp.fr;yahoo.fr;11343;0;0
friendcorp.fr;free.fr;9856;12;.12
friendcorp.fr;wanadoo.fr;9283;1;.01
messengear.fr;free.fr;9090;11;.12
messengear.fr;laposte.net;8107;2;.02
....................................
....................................
PHP code:
<?php
$PMTA_FILE = file_get_contents("text.txt");
$lineFromText = explode("\n", $PMTA_FILE);
$title = "";
$domain = "";
foreach($lineFromText as $line){
$data = explode(";",$line);
$domain = $data[0];
if (array_key_exists($domain, $domains_seen)){
continue;
}
$domains_seen[$domain] = true;
echo $domain;
echo "<br>";
echo $data[2];
echo "<br>";
}
?>
But the result of this code:
lebuzzdesbonsplans.com
26608
friendcorp.fr
11343
messengear.fr
9090
I do not want the result as above, I need the result as below:
lebuzzdesbonsplans.com
26608
24798
friendcorp.fr
11343
9856
9283
messengear.fr
9090
8107
Anyone know help me to get the solution please,Thanks.
You can use
$lines = array_reduce(file("text.txt", FILE_IGNORE_NEW_LINES), function ($a, $b) {
$b = str_getcsv($b, ";");
$a[$b[0]][] = $b[2];
return $a;
});
foreach($lines as $k => $values)
{
echo $k,PHP_EOL;
echo implode(PHP_EOL, $values);
echo PHP_EOL;
}
Output
lebuzzdesbonsplans.com
26608
24798
friendcorp.fr
11343
9856
9283
messengear.fr
9090
8107
natsort($lineFromText);
foreach($lineFromText as $line){
$data = explode(";",$line);
$domain = $data[0];
if (!array_key_exists($domain, $domains_seen)){
echo $domain;
echo "<br>";
}
echo $data[2];
echo "<br>";
$domains_seen[$domain] = true;
}
Update: sort the lines first - that way all the domains will be collected together,