I have a property table and it has six columns. Users upload photo and image name is stored in the column.
Now I want to count the number of columns for each row which are empty.
I am already able to do that but the code looks too long, I want to write efficient code, is there a way to rewrite the following efficiently.
while($data=$select->fetch()){
$imagecounter=0;
if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "" && $data['property_image6'] !== "") {
echo $imagecounter=6;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "") {
echo $imagecounter=5;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "") {
echo $imagecounter=4;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "") {
echo $imagecounter=3;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "") {
echo $imagecounter=2;
} else if ($data['property_image1'] !== "") {
echo $imagecounter=1;
}
}
You can do it like below:-
while($data=$select->fetch()){
$data1 = array($data['property_image1'],$data['property_image2'],$data['property_image3'],$data['property_image4'],$data['property_image5'],$data['property_image6']);
$count = count($data1); // count of original array
$count1 = count(array_filter($data1)); // remove empty indexes and count the values
echo "empty columns number is :-".($count-$count1);
}
Note:- $count1 is count of non-empty values
try this code
while($data=$select->fetch()):
$imagecounter = 0;
for($i=1; $i<=6; $i++)
if(!empty($data["property_image$i"]))
$imagecounter++;
echo $imagecounter;
endwhile;
does the column name has its rule?
assuming it's 'property_image{number}'
while($row=$result->fetch()) {
$count = 0;
for($i=0; $i<6; $i++) {
if($row['property_image'.$i]==NULL)
$count++;
}
echo "empty columns number is :-".($count);
}
code is not tested.
let me know if it doesnt work
Related
I've been searching and I've tried multiple ways with no luck, What's happening in my code looks for a link. It will then determine what type of link it is and adjust it to suit my needs. Once adjusted I need it to go into an Array with the end goal of adding that Array to my database. print_r($items) will be where ill be adding the array to the Database if you know how do do this also I will not say no to the help :D I am guessing it the same as adding a variable to the database.
Here is my code so far :)
//Lets look for links
$urlcomp = array();
$items = array();
foreach($html->find('a') as $element){
if( strpos( $element->href, "http" ) !== false) {
$urlcomp[] = $element->href;
// Look for gov websites
if ((strpos($urlcomp, 'gov') !== false) || (strpos($urlcomp, 'police') !== false) || (strpos($urlcomp, 'nhs') !== false) || (strpos($urlcomp, 'org') !== false) || (strpos($urlcomp, 'council') !== false)){
} else {
if (in_array($urlcomp, $websiteurlall)) {
}else{
echo "First Batch - " . $urlcomp;
$items[] = array($urlcomp);
echo "</br>";
}
}
}else{
$urlcomp = $websiteurlcomp.$element->href;
// Look for gov websites
if ((strpos($urlcomp, 'gov') !== false) || (strpos($urlcomp, 'police') !== false) || (strpos($urlcomp, 'nhs') !== false) || (strpos($urlcomp, 'org') !== false) || (strpos($urlcomp, 'council') !== false)){
} else {
if (in_array($urlcomp, $websiteurlall)) {
}else{
echo "Second Batch - " . $urlcomp;
$items[] = array($urlcomp);
echo "</br>";
}
}
}
}
print_r($items);
$urlcomp[] = $element->href; should have been $urlcomp = $element->href; that was the only issue :S
I am doing custom search for table. I have three search parameters: from, to and status. I have used eval() to filter result according to received parameter. Below is my code:
$search = ($from != "" || $to != "" || $status != "" );
if ($search) {
if ($from != '') {
$condition[] = '$from == $res["from_number"]';
}
if ($to != '') {
$condition[] = '$to == $res["to_number"]';
}
if ($status != '') {
$condition[] = '$status == $log["status"]';
}
$search = "if(" . implode(' && ', $condition) . '){ return false; } else { return true; }';
}
After getting the conditions I am using eval
if (eval($search)) {
}
My problem is I don't want to use eval(). It may cause security issues. Ladder if else is not possible, it would be very lengthy. Any other solution?
e.g. If i have passed value for status then i want check like
if($status == $log["status"]) {
}
if i have passed to & from number then it should be like:
if($from == $res["from_number"] && $to == $res["to_number"]) {
}
Don't use eval - it is potentially dangerous and not recommended to use.
Your code can be like this:
$result = false;
if ($from != "" || $to != "" || $status != "") {
if ($from != '' && $from != $res["from_number"]) $result = true;
if ($to != '' && $to != $res["to_number"]) $result = true;
if ($status != '' && $status != $log["status"]) $result = true;
}
if ($result) {
// ........
}
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';
}
I have a working code to restrict & validate subdomain.
$exp = explode('.', 'blog.mydomain.my.');
print_r($exp);
if(count($exp) == 3 && $exp[1] == "mydomain" && $exp[2] == "my" || $exp[3] == "") {
echo "<br>";
echo 'subdomain valid';
} else{
echo "<br>";
echo 'not valid';
}
now it need to check if its only false and I'm not so sure about the $exp[3] != "" comparison. From example below the subdomain should be valid but it give me error.
echo "<br>";echo "<br>";
$exp2 = explode('.', 'blog.mydomain.my.');
print_r($exp2);
if(count($exp2) != 3 || $exp2[1] != "mydomain" || $exp2[2] != "my" || $exp[3] != "") {
echo "<br>";
echo 'not valid';
}
Accepted numbers of subdomain is hello.mydomain.my or hello.mydomain.my. (with trailing dot). While hello.world.mydomain.my is not accepted.
Thanks in advance
This should give you what you want.
if(count($exp2) < 3 || count($exp2) > 4 || $exp2[1] != "mydomain" || $exp2[2] != "my" || (count($exp2) != 4 && $exp[3] != "")) {
echo "<br>";
echo 'not valid';
}
I would go for a regex solution, possibly even encapsulate it in a function:
function isValidDomain($domain)
{
return preg_match('/^[\w]+\.(mydomain)\.(my)\.?$/', $domain) ? true : false;
}
var_dump(isValidDomain('www.google.com'));
var_dump(isValidDomain('test.invalid.domain'));
var_dump(isValidDomain('hello.mydomain.my'));
var_dump(isValidDomain('hello.mydomain.my.'));
This works
If ($flag != 'u') { stuff.. }
This also works.
If ($id != 0) { stuff.. }
But these don't seem to work for me....
If ( ($flag != 'u') || ($id != 0) ) { stuff.. }
If ( ($flag != 'u') or ($id != 0) ) { stuff.. }
If ($flag != 'u') || ($id != 0) { stuff.. }
If ($flag != 'u') or ($id != 0) { stuff.. }
If ( $flag != 'u' || $id != 0 ) { stuff.. }
If ( $flag != 'u' or $id != 0 ) { stuff.. }
Any idea why? and what format should I use for this in PHP?
Your code actually works. I tried this
<?php
$flag= u;
$id= 0;
if ( ($flag != 'u') || ($id != 0) )
{
echo "Hi";
}
?>
The code simple says if $flag not equal to u or if $id not equals 0 then echo hi in page.
Is that your logic?
The problem might be in your logic I hope!