Combining PHP Variables - php

I've the following code:
if ($type == 'unit'){
$item_title = $result["title"];
}
elseif ($type == 'message'){
$item_title = $result["description"];
}
// all should combine unit and message
elseif ($type == 'all'){
$item_title = $result["description"]
$item_title .= $result["title"];
}
if (stripos( $item_title, $filter ) !== false || stripos( $item_title, $filter ) !== false)
How can I combine the unit and message results in the elseif ($type == all) statement?

I'm not sure what you mean, is this what you want?
elseif ($type == 'all'){
$item_title = $result["description"] . $result["title"];
}

You could create an associative array to store both....
elseif ($type == 'all') {
$item_title = array(
'description' => $result["description"],
'title' => $result["title"]
);
}
You can then access them with $item_title['description'] and $item_title['title'].
If you need to determine if $item_title is an array or not, use is_array().

Related

What is the best approach to scn directories inside condition

Inside a condition, I have some element in static.
$group == 'modules_account_customers'
for example
I would like to make that in dynamic (read).
How to proceed?
Below you an example of this condition.
All the directories start with modules_
I suppose I must use a function like a glob and select all the _modules. I don't know exactly how to start.
Any idea.
Tk.
foreach ( $modules_array as $module ) {
$class = basename($module, '.php');
// module language
if ( !class_exists($class) ) {
if ( MODE_VENTE_PRIVEE == 'true' && $customer->isLoggedOn() ) {
$modules_boxes = 'modules_boxes';
} elseif (MODE_VENTE_PRIVEE == 'true' && !$customer->isLoggedOn ) {
$modules_boxes = '';
} else {
$modules_boxes = 'modules_boxes';
}
if ($group == $modules_boxes ||
$group == 'modules_account_customers' ||
$group == 'modules_account_history_info' ||
$group == 'modules_advanced_search' ||
$group == 'modules_blog' ||
$group == 'modules_blog_content' ||
$group == 'modules_create_account' ||
) { .....}
}
}
I started by this and allow to display the directories inside an array :
public function getReadModulesDefaultDirectories($source_folder = 'modules_') {
$dir = $this->_directoryTemplateSources . '/' . $this->_template . '/' . $this->_codeSail . '/' . $this->_directoryModules;
$module_directories = array_diff(glob($dir . $source_folder . '*', GLOB_ONLYDIR), $exclude);
$result = [];
foreach ($module_directories as $value) {
$result[] = str_replace($dir, '', $value);
}
return $result;
}

i want to correct my recursive function in php

I have a recursive function
first of all one array of page URLs are passed to this function
then for each link and finds links in those pages, if those are pdf links then saved to $li array, if not then saved to $next array.
actually, in some pages, same links repeating.
so every time I want to pass $next array with new links only.but now $next array is storing all links and loop is not ending.please help
here is my code
<?php
global $next;
$next == array();
global $counter;
$counter = 2;
global $p;
$next is an array of page links got from a page
next_iter = get_html1($next);
function get_html1($next){
global $li;
global $all_links;
global $p;
$p++;
echo "count is" . $p . '<br>';
error_reporting(E_ERROR | E_PARSE);
global $URL;
global $counter;
$next = array_unique($next);
print_r($next);
foreach ($next as $nw) {
$page = '';
if ((strpos($nw, $URL) !== false ) && (strpos($nw, 'http://') !== false || strpos($nw, 'https://') !== false)) {
$page = $nw;
$html1 = file_get_html($page);
} else if (strpos($nw, $URL) === false && strpos($nw, 'http://') === false && strpos($nw, 'https://') === false) {
$page = $URL . $nw;
$html1 = file_get_html($page);
}
if ($html1 !== false) {
foreach ($html1->find('a') as $links) {
if (!in_array($links->href, $all_links)) {
if ($links->href != '#' && $links->href != NULL && $links->href != '/' && strpos($links->href, 'javascript') === false) {
$a = strip_tags($links->plaintext);
$a = preg_replace("/\s| /", '', $a);
$a = trim($a);
if ((preg_match('/.*\.pdf$/i', $links->href)) || (strcmp($a, 'Download') == 0) || stripos($a, 'Download') !== false) {
// echo $links->href;
if ((strpos($links->href, 'http://') !== false || strpos($links->href, 'https://') !== false)) {
$lm = $links->href;
if (!in_array($lm, $li)) {
write_to_folder($lm);
$li[] = $lm;
}
$li[] = $links->href;
} else if ((strpos($links->href, 'http://') === false && strpos($links->href, 'https://') === false)) {
// echo $URL . $links->href . '<br>';
$lm = $URL . $links->href;
if (!in_array($lm, $li)) {
write_to_folder($lm);
$li[] = $lm;
}
}
} else {
if (strpos($links->href, 'mailto:') === false && strpos($links->href, 'tel:') === false && strpos($links->href, '.zip') === false) {
if (!in_array($links->href, $next1)) {
$next[] = $links->href;
}
}
}
}
}
}
}
}
$next = array_unique($next);
echo "<br>next array";
if (!empty($next)) {
if ($p <= $counter) {
$recursiv = get_html1($next);
} else {
echo "SUCCESSFULLY EXECUTED";
exit;
}
} else {
echo "SUCCESSFULLY EXECUTED";
exit;
}
}
?>

Need help to add second condition to existing code

I am trying to add a second condition to existing code but it doesn't seem to be working.
The conditions are:
Compare two strings, from different arrays (working)
And check the value of a third string from a different array (not
working)
Here is the working code without the second condition: http://pastebin.com/bfpNb9zw
Here is my attempt:
Basically, the bit I am trying to get working is this part && ($ca = '') && ($ca = '0') && ($ca = '1') but it seems $ca is not able to be read outside the loop
if(!function_exists('lookup')){
function lookup($chain, $type) {
$cacount = count($chain['tbsCertificate']['extensions']);
for($j = 0; $j < $cacount; $j++) {
$count = count($chain['tbsCertificate'][$type]['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
$oid = array('id-at-commonName');
for($i = 0; $i < $count; $i++) {
foreach($exists as $field) {
if(
array_key_exists($field, $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value']) &&
in_array($chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['type'], $oid)
) {
$value = $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value'][$field];
return $value;
$ca = '';
if(isset($chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'])) {
$ca = $chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'];
}
}
}
}
}
return null;
}
}
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '1')) {
echo 'primary ca';
} else {
echo 'Root';
}
You are using =, which sets the value of $ca. You should be using === to check the value, instead.
Example:
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '1')) {
echo 'primary ca';
} else {
echo 'Root';
}

Hide group if user is not logged in and all items in group require login

I have a list of items, sorted into groups. When the user is not logged in, I print only the items that don't require login.
$previous_group = '';
foreach ($arr as $item) {
if($previous_group != $item['group']) {
// Add dividers
if($previous_group != '') echo '</ul>';
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
}
$previous_group = $item['group'];
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
echo '<li>'.$item['title'].'</li>';
}
}
echo '</ul>';
PhpFiddle
How can I hide the header for a group that doesn't have any items because the user isn't logged in? For example, the "food" category in the PhpFiddle example.
I could just go through the array twice, but is there a more efficient way to do it?
try
$previous_group = '';
foreach ($arr as $item) {
if($previous_group != $item['group'] && ($item['login_required'] == 'false' || $isLoggedIn == 'true')) {
// Add dividers
if($previous_group != ''){echo '</ul>';}
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
}
$previous_group = $item['group'];
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
echo '<li>'.$item['title'].'</li>';
}
}
echo '</ul>';
If you want just one iteration:
$previous_group = '';
$group_content = '';
$last_key = end(array_keys($arr));
foreach ($arr as $key => $item) {
if (!$previous_group) $previous_group = $item['group'];
if ($key === $last_key && $previous_group == $item['group'] && ($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false))) $group_content .= '<li>'.$item['title'].'</li>';
if ($group_content) {
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
echo $group_content;
echo '</ul>';
}
$previous_group = $item['group'];
$group_content = '';
}
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
$group_content .= '<li>'.$item['title'].'</li>';
}
}
However, doing more than one iteration will give you cleaner code.

php gettype fix

Following function I created is to store data in a database with the correct type.
I have several questions:
It makes sense to me to turn empty strings in to NULL. Are there consequences I am not considering?
What is a good max_length value for sqlite3 text. I know blob and text length constrains are the same, but are there any other considerations like displaying the data in a shell, etc.
Is there a simple way to check if data is an image, and what type, since mime is not available at this stage?
Does something stick out that I am missing?
public static function getType($value, $max_length = 50){
$type = gettype($value);
if($type == 'NULL'
|| $type == 'boolean'
|| $type == 'integer'
|| $type == 'double'
|| $type == 'object'
|| $type == 'resource'
|| $type == 'array'
)
return array('type'=>$type,'value'=>$value);
if($type == 'string' && empty($value))
return array('type'=>'NULL','value'=>$value);
if($type == 'string' && strlen($value) > $max_length)
return array('type'=>'blob','value'=>$value);
if($type == 'string' && substr($value, 0,1) === '0')
return array('type'=>'string','value'=>$value);
if($type == 'string' && is_numeric($value)){
$int = (int) $value;
$float = (float) $value;
if($int == $value){
$value = $int;
$type = 'integer';
}elseif($float == $value){
$value = $float;
$type = 'double';
}
}elseif($type == 'string'){
return array('type'=>$type,'value'=>$value);
}else{
$type = 'blob';
}
return array('type'=>$type,'value'=>$value);
}

Categories