i have a problem and i can't explain it ,,
first this is my function
function list_countries($id,$name=null,$result=null){
$countries = 'countries.txt';
$selected = '';
echo '<select name="'.$name.'" id="'.$id.'">';
echo '<option disabled>طالب الغد</option>';
if(file_exists($countries)){
if(is_readable($countries)){
$files = file_get_contents($countries);
$files = explode('|',$files);
foreach($files AS $file){
$value = sql_safe($file);
if(strlen($value) < 6){
echo '<option disabled>'.$value.'</option>';
}else{
if($value == $result){
$selected = ' selected="selected" ';
}
echo '<option value="'.$value.'".$selected.'>'.$value.'</option>';
}
}
}else{
echo 'The file is nor readable !';
}
}else{
echo "The file is not exist !";
}
echo '</select>';
}
Now the explain
i have a text file includes a countries names separated with "|"
In this file there is a heading before the countries ,, i mean Like this
U|United Kingdom|United State|UAE etc ..
L|Liberia|Libya etc ..
Now what the function Do is Disabled the Heading , and it's always one character ..
but the strlen function the minimum number that it's give to me is 5 not one .. " This is the first problem
The second one in the $result never equaled the $value and ether i don't know why ??
You need to split twice the file, one for the lines, one for the countries.
Also, since your "country header" is always the first item of each row, you do not need to check using strlen. Just shift out the first item of each row set: that one is the header, the following ones are the countries.
Something like this.
Note that in your code there is a syntax error in the echo that outputs the value, the > symbol is actually outside the quotes.
function list_countries($id,$name=null,$result=null){
$countries = 'countries.txt';
$selected = '';
$text = '<select name="'.$name.'" id="'.$id.'">';
$text .= '<option disabled>ﻁﺎﻠﺑ ﺎﻠﻏﺩ</option>';
if(file_exists($countries)){
if(is_readable($countries)){
$list = file($countries);
foreach($list as $item){
$item = trim($item);
$opts = explode('|', $item);
// The first item is the header.
$text .= "<option disabled>$opts[0]</option>";
array_shift($opts);
foreach($opts as $opt)
{
$value = sql_safe($opt);
$text .= '<option';
if($value == $result)
$text .= ' selected="selected"';
$text .= ' value="'.$value.'"';
$text .= '>'.$value."</option>\n";
}
}
}else{
$text .= "The file is not readable!";
}
}else{
$text .= "The file does not exist!";
}
$text .= '</select>';
return $text;
}
I have slightly modified your code so that the function actually returns the text to be output instead of echoing it; this makes for more reusability. To make the above function behave as yours did, just replace the return with
echo $text;
}
and you're good.
Related
Sorry if my question is being duplicate because I've tried to find similar questionI've got a data column in MySQL which look something like this :
https://i.stack.imgur.com/3VRI9.png
I've created a form which display the address in html using php.
$pdf_content .= '<div style="padding-left:5%">';
$pdf_content .= $company_name.'<br>';
$pdf_content .= '<div id="errorMessage">'.$address.'<br></div>';
//$pdf_content .= 'Add2,<br>';
//$pdf_content .= 'Add3,<br>';
$pdf_content .= $postcode.'<br>';
$pdf_content .= $state.'<br>';
$pdf_content .= $country.'<br>';
$pdf_content .= $mobile_phone.'<br>';
$pdf_content .= '<b>RE: Quotation for 3<sup>rd</sup> party claim vehicle </b>';
$pdf_content .= '</div><br>';
What I wanted to do now is the address which look something like No.43,Jalan Bandar Bahagia,Taman Pinji Mewah 1 , I wanted to separate the second comma which will look something like this
https://i.stack.imgur.com/0OlG3.png
I've tried but still not working
$pdf_content .= $address.'<br>';
$myList_explode = explode(",",$address);
for($i=0;$i<sizeof($myList_explode);$i++){
echo $myList_explode[$i];
if(($i+1)%2==0){
echo "</br>";
}else{
echo ",";
}
}
Is possible to do it in php while in MySQL it won't be separated?Thanks in advance.
Consume the leading substring before the second occurring comma, then forget it with \K. Then match the second occurring comma and replace it.
Regex prevents having to write a multi-line solution with a loop.
Code: (Demo)
echo preg_replace('~^[^,]*,[^,]*\K,~', '<br>', $address);
Output:
No.43,Jalan Bandar Bahagia<br>Taman Pinji Mewah 1
My pattern bears some similarity to the pattern in this preg_match() call: https://stackoverflow.com/a/65355441/2943403
You can try like this...:
$string = 'No.43,Jalan Bandar Bahagia,Taman Pinji Mewah 1';
echo formatString($string);
// No.43,Jalan Bandar Bahagia
// Taman Pinji Mewah 1
function formatString($inputString) {
$stringToArray = explode(",",$inputString);
$finalString = "";
$count = count($stringToArray) - 1;
foreach($stringToArray as $k => $arr) {
$addon = ",";
if($k == 1) {
$addon = "<br>";
}
if($k == $count) {
$addon = "";
}
$finalString .= $arr.$addon;
}
return $finalString;
}
Basically you create an array from string with explode, loop over it and create a string out of the elements. And since you want to add after second comma, function adds it if $key == 1 (second key).
I am trying to select a previously selected value inside dropdown selection using php. My code is as follows
<?php
$selected_val = "Assistant Professor";
function generateSelect($type = 'text', $name = 'desgntn', $id = 'fac_dsgn', $options = array(), $default) {
$html = '<select type="'.$type.'" name="'.$name.'" id="'.$id.'">';
foreach ($options as $option ) {
if ($option == $default) {
$html .= '<option value='.$option.' selected="selected">'.$option.'</option>';
} else {
$html .= '<option value='.$option.'>'.$option.'</option>';
}
}
$html .= '</select>';
return $html;
}
$list = array("Lecturer", "Assistant Professor", "Associate Professor", "Professor", "Assistant Professor (On Leave)", "Associate Professor (On Leave)", "Professor (On Leave)");
$res = generateSelect('text', 'desgntn', 'fac_dsgn', $list, $selected_val);
echo $res;
?>
But the problem is I am always getting the first word of a multi spaced option inside value attributein option tag of html. for example: I am getting Assistant inside value tag for Assistant Professor. Is there any way so that I can always get the actual value with spaces?
I believe you forgot to enclose the option value in quotes:
it should be something like
$html .= '<option value="'.$option.'" selected="selected">'.$option.'</option>';
(note the double quotes around $option)
Hope it helps
if $text isnt allowed list print error. Dont know how do do it
$allowedDomains = array('www.test1.com', 'www.test2.co.uk', 'test3.com');
$text = 'this is my url www.abcd.com/index.php?page=home';
try like this
$allowedDomains = array('www.test1.com', 'www.test2.co.uk', 'test3.com');
if ( !in_array('your-domain-name', $allowedDomains) {
echo 'show your error here';
} else {
echo 'do what ever you want here';
}
Here's another way of doing it: create a regex using the list of domains, and then use that to test your text strings.
$allowedDomains = array('www.test1.com', 'www.test2.co.uk', 'test3.com');
# concatenate the allowed domains with | and surround with brackets so the regex will
# match any of the array items
# i.e. $regex is (www.test1.com|www.test2.co.uk|test3.com)
$regex = "(" . implode("|", $allowedDomains) . ")";
# a couple of test strings
$texts = array('this is my url www.abcd.com/index.php?page=home',
'my home page is www.test3.com');
foreach ($texts as $t) {
if (preg_match("/$regex/", $t)) {
echo "Found a match! $t\n";
}
else {
echo "No match found: $t\n";
}
}
Output:
No match found: this is my url www.abcd.com/index.php?page=home
Found a match! my home page is www.test3.com
I've written a function for you that'll allow you to add / remove domain names and / or domain extensions as you please, taking into account that not everyone types "http(s)" and / or "www" into their domain searches and also the unpredictability of what kind of information might be behind the domain or not:
<?php
function testDomain($domain){
//Set domain names
$arrDomainNames = array("test1","test2","test3");
//Set domain extensions like .com .co .info
//Do NOT add .co.uk! .uk is taken care of in the regex
$arrDomainExt = array("com","co");
$cntDomainNames = count($arrDomainNames);
$cntDomainExt = count($arrDomainExt);
$i = 0;
$y = 0;
$DomNames = "";
$DomExt = "";
foreach($arrDomainNames as $value){
if($i == $cntDomainNames - 1){
$DomNames .= $value;
} else {
$DomNames .= $value ."|";
}
$i++;
}
unset($value);
foreach($arrDomainExt as $value){
if($y == $cntDomainExt - 1){
$DomExt .= $value;
} else {
$DomExt .= $value ."|";
}
$y++;
}
unset($value);
$pattern = "/((((http|ftp|https)+(:)+(\/{2})))?+(www\.)?(((". $DomNames .")+\.)+(". $DomExt .")+(\.uk)?(:[0-9]+)?((\/([~0-9a-zA-Z\#\+\%#\.\/_-]+))?(\?[0-9a-zA-Z\+\%#\/&\[\];=_-]+)?)?))\b/imu";
if(preg_match($pattern, $domain)){
echo "Domain match!<br />";
} else {
echo "Domain not match!<br />";
}
}
testDomain("www.test1.com"); //match
testDomain("test1.com"); //match
testDomain("http://www.test1.co.uk"); //match
testDomain("test1.com/info.php?who=you"); //match
testDomain("www.google.com"); //no match
?>
How to make it working with
$arrDomainNames = array("test1.com","test2.org","test3.co.uk","test3.net");
without
$arrDomainExt = array("com","co");
One of my clients websites, www.kevinsplants.co.uk is not showing the search results in alphabetical order, how do I go about ordering the results in alphabetical order?
We are using the Shopp plugin and I believe its that plugin that is generating the results!
Cheers, Brad
case "orderby-list":
if (isset($Shopp->Category->controls)) return false;
if (isset($Shopp->Category->smart)) return false;
$menuoptions = Category::sortoptions();
$title = "";
$string = "";
$default = $Shopp->Settings->get('default_product_order');
if (empty($default)) $default = "title";
if (isset($options['default'])) $default = $options['default'];
if (isset($options['title'])) $title = $options['title'];
if (value_is_true($options['dropdown'])) {
if (isset($Shopp->Cart->data->Category['orderby']))
$default = $Shopp->Cart->data->Category['orderby'];
$string .= $title;
$string .= '<form action="'.esc_url($_SERVER['REQUEST_URI']).'" method="get" id="shopp-'.$Shopp->Category->slug.'-orderby-menu">';
if (!SHOPP_PERMALINKS) {
foreach ($_GET as $key => $value)
if ($key != 'shopp_orderby') $string .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
}
$string .= '<select name="shopp_orderby" class="shopp-orderby-menu">';
$string .= menuoptions($menuoptions,$default,true);
$string .= '</select>';
$string .= '</form>';
$string .= '<script type="text/javascript">';
$string .= "jQuery('#shopp-".$Shopp->Category->slug."-orderby-menu select.shopp-orderby-menu').change(function () { this.form.submit(); });";
$string .= '</script>';
} else {
if (strpos($_SERVER['REQUEST_URI'],"?") !== false)
list($link,$query) = explode("\?",$_SERVER['REQUEST_URI']);
$query = $_GET;
unset($query['shopp_orderby']);
$query = http_build_query($query);
if (!empty($query)) $query .= '&';
foreach($menuoptions as $value => $option) {
$label = $option;
$href = esc_url($link.'?'.$query.'shopp_orderby='.$value);
$string .= '<li>'.$label.'</li>';
}
}
return $string;
break;
http://wordpress.org/extend/plugins/sort-searchresult-by-title/
This is a plug-in that seems to do what you want.
You need to edit the template file of the search page and add to the search query parameters orderby title, as opposed to the default ordering which is by time of publication
It is not a good idea to edit the core code. And lucky for you it is not necessary. You can edit/create your own template file collection-search.php and style it any way you want.
There is more info on template files here.
I have an array coming from a .csv file. These are coming from a real estate program. In the second column I have the words For Sale and in the third column the words For Rent that are indicated only on the rows that are concerned. Otherwise the cell is empty. I want to display a list rows only For Sale for example. Of course then if the user clicks on a link in one of the rows, the appropriate page will be displayed.
I can't seem to target the text in the column, and I can't permit that the words For Sale be used throughout the entire array because they could appear in another column (description for example).
I have tried this, but to no avail.
/* The array is $arrCSV */
foreach($arrCSV as $book) {
if($book[1] === For Sale) {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
I also tried this:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
Do you mean:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div></div>';
}else{
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
}
I found a solution here:
PHP: Taking Array (CSV) And Intelligently Returning Information
$searchCity = 'For Sale'; //or whatever you are looking for
$file = file_get_contents('annonces.csv');
$results = array();
$lines = explode("\n",$file);
//use any line delim as the 1st param,
//im deciding on \n but idk how your file is encoded
foreach($lines as $line){
//split the line
$col = explode(";",$line);
//and you know city is the 3rd element
if(trim($col[1]) == $searchCity){
$results[] = $col;
}
}
And then:
foreach($results as $Rockband)
{
echo "<tr>";
foreach($Rockband as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
}
As you can see I'm learning. It turns out that by formulating a question, a series of similar posts are displayed, which is much quicker and easier than using google. Thanks.