Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a form in my website where users can send messages to multiple recipients, they type in the recipient's numbers separated by commas, I recieve the numbers in my php script as a single post variable, but I need to add the recipient,s area code on front of every number, I have tried to explode the variable using the comma as a separator but I do not know what to do from there
$recipient=$_POST['recipient'];
$numbers=explode(',' $recipient);
for ($i = 0; $i <=sizeof($numbers); $i++) {
}
I do not know how you're getting the area codes in the script, but to prepend the number with the area code, you can just use string concatenation, like so:
$areacode = 123; // this is received dynamically
$result = array(); // initialize empty array
for ($i = 0; $i <= sizeof($numbers); $i++) {
$result[] = $areacode . '_' . $numbers[$i];
}
This can also be done using a foreach:
foreach (explode(',', $recipient) as $number) {
$result[] = $areacode . '_' . $number;
}
Now, if you want to get this back as a comma-separated string, you can use implode(), like so:
$result_string = implode(',', $result);
The short answer is something like:
$numbers_with_area_codes = Array();
for ($i = 0; $i <= sizeof($numbers); $i++) {
$numbers_with_area_codes[] = '+372'.$numbers[$i];
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to print the below statement.
Thanks for updating your PH_NUMBER, ADDRESS, OFFICE NUMBER and EMAIL.
Where PH_NUMBER, ADDRESS, OFFICE_NUMBER and EMAIL are variables.
For example, if value is empty for variable "OFFICE_NUMBER" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and EMAIL.
For example, if value is empty for variable "EMAIL" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and OFFICE NUMBER.
$a = "Address";
$b = "Mobile";
$c = "email";
$array = array($a, $b, $c);
$length = count($array);
for ($i = 0; $i < $length; $i++) {
$total = $array[$i];
}
echo "Thanks for udpating " . $total ;
How can this be done ?
Any suggestions would be helpful.
You don't need to use a loop.
<?php
/* Collect all parameters to the array */
$variables = [
'PH_NUMBER',
'ADDRESS',
'EMAIL',
'OFFICE_NUMBER',
];
/* Sorting the array if need */
/* Pop the element off the end of array */
$variables = array_filter($variables);
$count = count($variables);
$lastVariable = array_pop($variables);
if($count > 1) {
printf('Thanks for updating your %s and %s', implode(', ', $variables), $lastVariable);
}elseif($count === 1){
printf('Thanks for updating your %s', $lastVariable);
}else{
print 'Nothing for update';
}
?>
Update: Small improvement in order to catch cases with only 1 variable and empty array (nothing for update).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to retrieve some results from the database based on some letters known.
Here is my PHP script:
$sql = "SELECT * FROM lexem WHERE charLength = '$total'";
$doh = "__a_d";
$sql .= " AND formUtf8General LIKE '$doh'";
The length, letters, letters number and letters position are variable.
My script only returns results that contain some or one of my letters. How can I improve it?
I hope that i have understood correctly. I really want you to try the code below. It uses an array containing both letters and their position in the SQL parameter.
$filter_string = '';
$offset = 0;
$total = 10; //or any other number
$escaped_characters = array('_', '%'); //Any other MySQL special characters
$filters = array(
3=>'a', 5=>'d', 9=>'%', 11=>'r', 0=>'n', 2=>'k', 1=>'w', -1=>'t',
);
ksort($filters);
foreach($filters as $key => $value) {
if ($key < 1 || strlen($value) != 1) {
continue;
}
for($i = 1; $i < ($key - $offset); $i++) {
$filter_string .= '_';
}
if (in_array($value, $escaped_characters)) {
$filter_string .= '\\'.$value;
} else {
$filter_string .= $value;
}
$offset = $key;
}
//Code that escapes parameters in query
$sql = "SELECT * FROM lexem WHERE charLength = '$total' AND formUtf8General LIKE '$filter_string%';";
The $filters array is the one that has the positions of the characters in a key => value format (one character each time). We need to sort it by key so the offset variable will take the correct values each time in the foreach loop. In case you need characters that are also MySQL special characters then $escaped_characters array is useful.
Important: You have to escape all query parameters before you run queries like that because of security reasons (SQL Injection).
Additional info: How can I prevent SQL injection in PHP?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was stuck at some point. I want your little help. I need to find the navigation pages of a website and put it a for loop. I have done it hard coded but I need it dynamically .
Here are the examples:
<?php
for ($x = 1; $x <= 5; $x++) {
$olxcom = file_get_contents('http://olx.com.pk/cars/?page=' . $x . '');
$file1 = 'olx.txt';
file_put_contents($file1 , $olxcom, FILE_APPEND);
}
for ($y = 1; $y <= 5; $y++) {
$pakwheels = file_get_contents('http://www.pakwheels.com/used-cars/search/-/?page=' . $y . '');
$file2 = 'pakwheels.txt';
file_put_contents($file2 , $pakwheels, FILE_APPEND);
}
for ($z = 1; $z <= 5; $z++) {
$carmudi = file_get_contents('http://www.carmudi.pk/cars/?page=' . $z . '');
$file3 = 'carmudi.txt';
file_put_contents($file3 , $carmudi, FILE_APPEND);
}
?>
These number 5 is the page number which I hardcoded. I am trying to find those with a for loop or something else.
Thanks
Try: than pass to your array;
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node)
{
echo $node->nodeValue.': '.$node->getAttribute("href")."\n";
}
Use an multi demensional array(); http://php.net/manual/en/language.types.array.php
Example:
<?php
$my_array = array('x','y','z');
for ($row = 0; $row < 3; $row++) {
echo "<p> your stuff item ". $row."<p>";
for ($col = 0; $col <5; $col++) {
echo "the inner stuff";
}
}
?>
This should solve your issue...
You need to download and parse the html of a page to find the page number. Try Simple HTML DOM Parser .
//random example of selecting from html content
$ret = $html->find('div.foo');
//OR
$ret = $html->find('div[class=foo]');
Basically you can access elements, like how you use css selectors. Find the navigation id of the webpage and parse the number of pages from it.
Check How to find HTML elements? section, tab Advanced of page.
For example http://www.carmudi.pk/cars/ contains the total number of pages in
<li class="total-pages"> of <strong>1036</strong> </li>
You can access it using the class total-pages and parsing the text.
If you are looking for a generic solution that works on all websites, as each website has different html for navigation, each requires parsing separately.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Kindly someone explain to me about this?
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
is faster than
foreach($aHash as $key=>$val) $aHash[$key] .= "a";
According to The PHP Benchmark. However I have a a code in my script:
My CODE:
foreach($_SESSION['undo'] as $key2=>$value2)
{
if{
}
else
{
}
.
.
.
.
}
How can I convert a code like that as shown above to my code?
Kindly explain why? Thank you.
do not count in for condition
you can try this
$size = count($_SESSION['undo']);
for($i = 0; $i< $size; $i++){
$value = $_SESSION['undo'][$i];
}
In a foreach loop the first part is your array and the second part after the as is the current value when iterating.
When using a for loop you are working with indexes and have to manually access them. Just do the same as in your example. I'm assuming you are working with an associative array as you are using array keys.
$myArray = $_SESSION['undo'];
$keys = array_keys($myArray);
$size = sizeOf($keys);
for ($i = 0; $i < $size; $i ++) {
/* do something with $myArray[$keys[$i]] */
echo $myArray[$keys[$i]];
}
try this:
for($i = 0; $i< count($_SESSION['undo']); $i++){
$value = $_SESSION['undo'][$i];
}
Seems you made a mistake, from the http://www.phpbench.com/,
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
This way cost 92 us, just foreach cost 16 us, with 'as' cost 21 us.
Guys, wake up....
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose I have a string such as this:
ABC.DEF.GHI.JKL (This string could have any length and amount of characters between dots)
I want to add the following combinations into an array.
ABC
ABC.DEF
ABC.DEF.GHI
ABC.DEF.GHI.JKL
So basically the string should be split with the dot character and then the individual sub-strings should be combined.
I have a function already, but in my opinion this seems too complicated to achieve the result.
$myarray = array();
$split = explode('.', 'that.string.with.dots');
$string = '';
for ($i = 0; $i < count($split); $i++) {
$string .= $split[$i];
myarray[] = $string;
$string .= '.';
}
Any suggestions on improving this?
Thanks
Michael
Perhaps this:
$split = explode('.', 'that.string.with.dots');
for ($i = 1; $i < count($split); $i++) {
$split[i] = $split[$i-1] . '.' . $split[i];
}
It just concats the current with the previous.
I have the feeling that you are asking if there is a PHP function that will achieve this result with a single function call, but I don't think there is one.
You can rewrite the code as pritaeas did in his answer, to remove two lines of code, but it will still be the same concept.