CodeIgniter script not working properly - php

function displayList() {
$str = '';
$query = $this->db->query("SELECT * FROM data");
foreach ($query->result() as $row) {
$b = '<input name="completed" type="checkbox" />';
$a = $row->title;
$str = $b.$a;
}
return $str;
}
This script is only displaying the last field in the database. Why is this?

Because you're not concatenating, you're reassigning. Do this:
$str .= $b.$a;
Otherwise the loop overwrites $str each time it runs, which explains why you're only seeing the last result.

it should be $str .= $b.$a;
You overwrite $str each time instead of adding new string at the end

It is overwriting:
$str = $b.$a;
This string changes every loop again. If you want to make it an array, do this
$str[] = $b.$a;
If you want to add it to the text:
$str .= $b.$a;

Related

multiple characters replacements in string php

Let's say that I have the following string:
$string = 'xxyyzz';
And then I have a substitution array like this:
$subs = ['xy'];
Meaning that every x should be replaced by y in my string and every y should be replaced by x. Let's say that my substitution array can only contain pairs of characters to be replaced in my $string.
How would I go about doing this?
I tried using str_replace the following way but that doesn't work:
foreach ($subs as $sub) {
$sub_arr = str_split($sub);
$reversed_sub_arr = array_reverse($sub_arr);
$output = str_replace($sub_arr, $reversed_sub_arr, str_split($string));
}
$output = implode('', $output);
But the output gives me xxxxzz
The output should be yyxxzz
Thanks for any help
This working for your case
$string = 'xxyyzz';
$subs = ['xy'];
foreach ($subs as $sub) {
$sub_arr = str_split($sub);
$output = strtr($string, array($sub_arr[0]=>$sub_arr[1], $sub_arr[1]=>$sub_arr[0]));
}
echo $output; //yyxxzz
Extending #Orgil answer if two items in $subs array like $subs = ['xy', 'dz']
$string = $output = 'xxyyzz';
$subs = ['xy', 'dz'];
foreach ($subs as $sub) {
$sub_arr = str_split($sub);
$output = strtr($output, array($sub_arr[0]=>$sub_arr[1], $sub_arr[1]=>$sub_arr[0]));
}
echo $output;
Demo

Can not to replace string

The problem is related to php. I have a text in a variable say $text and some start position and end positions. How to inject the some substring on the specific position. the problem is that if i put some text in a specific position then position index changes and it will be difficult to put next sub string to the right place. Help me. thanks in advance.
function previewTxt($text,$topicid, $sectionid)
{
$dbConn = new DBConnUtil();
$queryString = "SELECT * from inline_topic_xref where TOPIC_ID=$topicid and SECTION_ID=$sectionid";
$result = $dbConn->run_query($queryString);
$newtext=$text;
$count=0;
while($row = $result->fetch_object())
{
$newtext = replace($newtext, "<a href='#'>" ,$row->REF_TEXT_START);
$newtext = replace($newtext, "</a>" ,$row->REF_TEXT_END);
}
return $newtext;
}
function replace($org_text,$str_rep, $position)
{
$length=strlen($org_text);
$temp1=substr($org_text,0,$position);
$temp2=substr($org_text,$position,$length);
$replaced =$temp1.$str_rep.$temp2;
return $replaced;
}
have you tried
substr_replace($oldstr, $str_to_insert, $pos, 0)
?
here is the documentation
http://php.net/manual/en/function.substr-replace.php

Replace a delimiter by html tag in php

I am trying to find a way to replace a string as the following:
Original string:
|Hello||everybody|, I am |human|
And result:
<span>Hello</span><span>everybody</span>, I am <span>human</span>
Is there a simple way to replace this original string to this result.
Thanks in advance.
preg_replace(
"~\|(.+)\|~U",
"<span>$1</span>",
$yourString
);
ideone demo.
I'm not really good at regexp, so here is an other code:
$string = "|Hello||everybody|, I am |human|";
$arr = explode("|", $string);
$result = "";
$span = "<span>";
$span_close = "</span>";
foreach($arr as $element){
if(strlen($element) > 0){
$result .= $span.$element.$span_close;
}
}
echo $result;

Extracting a specific part of a string

I have this string:
[25-03-15, 1236], [26-03-15, 3000], [27-03-15, 3054], [30-03-15, 4000]
I want to get two parts from it as below:
['25-03-15','26-03-15','27-03-15','30-03-2015']
and
[1236,3000,3054,4000]
Please guide me how I can perform this task.
PS: I am working of view page of codeigniter.
I'm getting the first thing as:
<?php
$usd=$this->db->query('select transaction_date, SUM(amount) as total
from transactions GROUP BY transaction_date')->result_array();
$str = '';
for($i=0; $i<count($usd); $i++){
if($i!=0){
$str = $str.', ['.date('d-m-y', strtotime($usd[$i]["transaction_date"])).', '.$usd[$i]["total"].']';
}else{
$str = $str.'['.date('d-m-y', strtotime($usd[$i]["transaction_date"])).', '.$usd[$i]["total"].']';
}
}
echo $str;
?>
Try this..
$date = '';
$total = '';
for($i=0; $i<count($usd); $i++){
if($i!=0){
$date .= date('d-m-y', strtotime($usd[$i]["transaction_date"])).', ';
$total .= $usd[$i]["total"].', ';
}else{
$date .= date('d-m-y', strtotime($usd[$i]["transaction_date"])).', ';
$total .= $usd[$i]["total"].', ';
}
}
echo $finaldate='['. $date.']';
echo $finaltotal='['. $total.']';
I don't see any reason why you need to save this data from your db in a string. Just save it in an array and it is that easy:
So here I save your data in an array, so that you get this structure:
array(
array(25-03-15, 1236),
array(26-03-15, 3000),
array(27-03-15, 3054),
array(30-03-15, 4000)
)
The you can simply use array_column() to extract the single columns, like this:
<?php
$usd = $this->db->query('select transaction_date, SUM(amount) as total
from transactions GROUP BY transaction_date')->result_array();
foreach($usd as $k => $v)
$result[] = [date('d-m-y', strtotime($usd[$k]["transaction_date"])), $usd[$k]["total"]];
$partOne = array_column($result, 0);
$partTwo = array_column($result, 1);
?>
Also if you then need this data in a string as you said in the comments you can simply transform it:
I will pass this extracted data to a graph that accepts this kind of data – Shahid Rafiq 6 mins ago
Just use this:
echo $str = "[" . implode("],[", array_map(function($v){
return implode(",", $v);
}, $usd)) . "]";
output:
[25-03-15, 1236], [26-03-15, 3000], [27-03-15, 3054], [30-03-15, 4000]
EDIT:
If you also want the parts as string just simply do this:
$partOne = "[" . implode("],[", array_column($arr, 0)) . "]";
$partTwo = "[" . implode("],[", array_column($arr, 1)) . "]";
output:
[25-03-15],[26-03-15],[27-03-15],[30-03-15]
[1236],[3000],[3054],[4000]

i want text and numeric part from the string in php

i have one string
$str ='california 94063';
now i want california and 94063 both in diferent variable.
string can be anything
Thanks in advance....
How about
$strings = explode(' ', $str);
Assuming that your string has ' ' as a separator.
Then, if you want to find the numeric entries of the $strings array, you can use is_numeric function.
Do like this
list($str1,$str2)=explode(' ',$str);
echo $str2;
If your string layout is always the same (say: follows a given format) then I'd use sscanf (http://www.php.net/manual/en/function.sscanf.php).
list($str, $number) = sscanf('california 94063, "%str %d");
<?php
$str ='california 94063';
$x = preg_match('(([a-zA-Z]*) ([0-9]*))',$str, $r);
echo 'String Part='. $r[1];
echo "<br />";
echo 'Number Part='.$r[2];
?>
If text pattern can be changed then I found this solution
Source ::
How to separate letters and digits from a string in php
<?php
$string="94063 california";
$chars = '';
$nums = '';
for ($index=0;$index<strlen($string);$index++) {
if(isNumber($string[$index]))
$nums .= $string[$index];
else
$chars .= $string[$index];
}
echo "Chars: -".trim($chars)."-<br>Nums: -".trim($nums)."-";
function isNumber($c) {
return preg_match('/[0-9]/', $c);
}
?>

Categories