Format string in PHP [duplicate] - php

This question already has answers here:
How to get the last dir from a path in a string
(11 answers)
How to obtain the last word of a string
(8 answers)
Closed 7 years ago.
I have below code, which gets me file path as voicetube/record/1985150721112615
but I need only 1985150721112615 exploding first two folders,please help me on this
$sql = "SELECT filePath FROM user_recordings ORDER BY recordDate ASC";
$result = mysql_query($sql);
$var = array();
while ($row = mysql_fetch_array($result))
{
$var[] = $row['filePath'];
}

You are looking for basename();
$var[] = basename($row['filePath']);

You can use explode function, and grab it's last index.
while ($row = mysql_fetch_array($result))
{
$arr = explode('/', $row['filePath']);
$var[] = $arr[count($arr) -1];
}

Related

Two foreach use with a array [duplicate]

This question already has answers here:
How can I loop through a MySQL result set more than once using the mysql_* functions?
(7 answers)
PDO multiple fetching of same query [duplicate]
(2 answers)
Closed 3 years ago.
I have a array and i use two foreach but I be only able to print the first foreach.
$test = $db->query("SELECT * FROM Test");
foreach ($test as $readTest) {
echo $readTest["col1"];
}
foreach ($test as $readTest) {
echo $readTest["col2"];
}
and I try with this:
$test1 = $test;
$test2 = $test;
I expect the output of $readTest["col1"] and $readTest["col2"], but the actual output is $readTest["col1"]
Use #fetchAll to get all your result in a conventional array. You will then be able to loop on them multiple times.
Why use array, when you can print this way:
$test = $db->query("SELECT * FROM Test");
while($PrintTest = $test->fetch_object())
{
echo $PrintTest->col1;
echo $PrintTest->col2;
}
Even if you want to use Arrays, you can do something like this:
$Array1 = array();
$Array2 = array();
$test = $db->query("SELECT * FROM Test");
while($PrintTest = $test->fetch_object())
{
$Array1[] = $PrintTest->col1;
$Array2[] = $PrintTest->col2;
}
And then, you can run through the array with For or Foreach

last element in while loop php [duplicate]

This question already has answers here:
Removing last comma from a foreach loop
(9 answers)
Closed 5 years ago.
$result = $conn->query("SELECT * FROM mails");
while($row = $result->fetch_assoc()){
$mymail= $row['mail']. ",";
echo $mymail;
}
I have after every row comma (,) but I don't want (,) after last row. How could I do this?
Take a look at the join function:
$mails = array();
$result = $conn->query("SELECT * FROM mails");
while($row = $result->fetch_assoc())
{
$mails[] = $row['mail']; // Store E-Mail adresses in an array
}
$commaSeparatedMails = join(',', $mails); // Connect all array parts using a comma
echo $commaSeparatedMails;
rtrim(yourstring,","), this function will remove right comma.

get rid of last comma in a loop in php [duplicate]

This question already has answers here:
PHP Mysql WHILE loop remove the last separator
(4 answers)
Closed 6 years ago.
I'm trying to send mulitple sms and the correct format for the api to send the text messages should be (00,001,002) but with my current sql it produces this results (00,001,002,)
How can i correct my script to get this results (00,001,002)
$query_rs_p = "SELECT * FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
$totalRows_rs_p = mysql_num_rows($rs_p);
$ph=',';
do {
echo $row_rs_p['phone_number'].$ph;
} while ($row_rs_p = mysql_fetch_assoc($rs_p));
Instead of echoing it out in your loop, put it all in a string. Then trim off the last comma before you output it:
$query_rs_p = "SELECT * FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
$totalRows_rs_p = mysql_num_rows($rs_p);
$ph=',';
$string = '';
do {
$string .= $row_rs_p['phone_number'].$ph;
} while ($row_rs_p = mysql_fetch_assoc($rs_p));
$string = rtrim($string, ',');
echo $string;
Or, better yet, just build this string in your query. Much simpler and cleaner:
$query_rs_p = "SELECT group_concat(phone_number) as phine_numbers FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
echo $row['phone_numbers'];
Use rtrim function
$rowsphone = $row_rs_p['phone_number'].$ph;
rtrim($rowsphone,',');
Probably the most direct would be to just do something like this:
$output = '';
do {
if ($output != '')
$output .= ',';
$output .= $row_rs_p['phone_number'];
while ($row_rs_p = mysql_fetch_assoc($rs_p);
echo $output;

How do I make a variable increment by adding a counter variable to the end inside a while loop [duplicate]

This question already has answers here:
creating variable name by concatenating strings in php
(4 answers)
Closed 7 years ago.
I am trying to get a variable to increment by adding a counter to the end of it. Here is the code.
$counter = 0;
while($results = $query->fetch_assoc()){
$var . ++$counter = $results['row'];
} // while loop
My target outcome...
$var1 = result 1
$var2 = result 2
$var3 = result 3
It is trying to set $counter = result.
To make a variable variable you can use {}:
$counter = 0;
while($results = $query->fetch_assoc()){
${"var".++$counter} = $results['row'];
} // while loop

ksort on multidimensional array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 9 months ago.
I have a multidimensional array, and I need to sort that array by a specific key in that array.
I add to the array like this in a for loop
$myArr[$i][0] = $row[1];
$myArr[$i][1] = $row[2];
$myArr[$i][2] = $row[3];
Now lets say that the value of $row[3] is DATE_ATOM.
How can i arrange the completed array by $myArr[$i][2]?
Thanks!
What you are probably looking for is array_multisort(), specifically this example usage (Sorting database results).
For example (based on your code above):
$i = 0;
$myArr = $col1 = $col2 = $col3 = array();
foreach ($rows as $row) {
$myArr[$i][0] = $col1[$i] = $row[1];
$myArr[$i][1] = $col2[$i] = $row[2];
$myArr[$i][2] = $col3[$i] = $row[3];
$i++;
}
array_multisort($col3, SORT_ASC, $myArr);
var_dump($myArr);

Categories