I'm trying to show the progress of a user filling out their user profile by an image sequence.
Example, if the user fills out their name, progress image 2 will show up.
If the user fills out their name and bio, progress image 3 will appear, and so on....
I'm trying to use if and elseif statements, but only progress image 2 will appear.
So basically, I just want one image to show based on the progress the users has made on completing the user profile form.
Here is my code.
<?php
$name = ($data->first_name);
$bio = ($data->bio);
$address = ($data->street1);
$phone = ($data->phone);
$ec = ($data->e_contact);
$ecp = ($data->e_phone);
$bs = ($data->bs);
if($name == true){?>
<img src="../images/progress2.png" class="img-max-progress">
<?php }
elseif($name.$bio == true){?>
<img src="../images/progress3.png" class="img-max-progress">
<?php
}
elseif($name.$bio.$address == true){?>
<img src="../images/progress4.png" class="img-max-progress">
<?php
}
elseif($name.$bio.$address.$phone == true){?>
<img src="../images/progress5.png" class="img-max-progress">
<?php
}
elseif($name.$bio.$address.$phone.$ec == true){?>
<img src="../images/progress6.png" class="img-max-progress">
<?php
}
elseif($name.$bio.$address.$phone.$ec.$ecp == true){?>
<img src="../images/progress7.png" class="img-max-progress">
<?php
}
elseif($name.$bio.$address.$phone.$ec.$ecp.$bs == true){?>
<img src="../images/progress8.png" class="img-max-progress">
<?php
}
?>
<?php
if($name != true){?>
<img src="../images/progress1.png" class="img-max-progress">
<?php
}
?>
I have the form filled out so progress8.png should be the image that is shown,
but progress2.png is currently showing.
Using a combo between #user3132781 and #vlzvl answers, I got it working like this.
<?php
$name = ($data->first_name);
$bio = ($data->bio);
$address = ($data->street1);
$phone = ($data->phone);
$ec = ($data->e_contact);
$ecp = ($data->e_phone);
$bs = ($data->bs);
$completed = 0;
if (empty($name)) {
$completed += 1;
}
if (!empty($name)) {
$completed += 2;
}
if (!empty($bio)) {
$completed += 1;
}
if (!empty($address)) {
$completed += 1;
}
if (!empty($phone)) {
$completed += 1;
}
if (!empty($ec)) {
$completed += 1;
}
if (!empty($ecp)) {
$completed += 1;
}
if (!empty($bs)) {
$completed += 1;
}
echo '<img src="../images/progress' . $completed .'.png" class="img-max-progress">';
?>
I have an other approach - just as a suggestion:
$completed = 0;
if (isset($name)) {
$completed += 1;
}
if (isset($adress)) {
$completed += 1;
}
..... and so on for all the fields
//$completed can be used for defining the image
echo '<img src="../images/progress' . $completed .'.png" class="img-max-progress">';
Hmmm.. I would try something along the lines of:
if($name){
img 1
}
elseif ($name && $bio){
img 2
}
etc.
The final line would be:
if(!$name){
img progress1
}
Try this (it is also a lot cleaner):
<?php
$name = ($data->first_name);
$bio = ($data->bio);
$address = ($data->street1);
$phone = ($data->phone);
$ec = ($data->e_contact);
$ecp = ($data->e_phone);
$bs = ($data->bs);
if($name == true) {
echo '<img src="../images/progress2.png" class="img-max-progress">';
}
elseif($name== true && $bio == true) {
echo '<img src="../images/progress3.png" class="img-max-progress">';
}
elseif($name== true && $bio == true && $address == true) {
echo '<img src="../images/progress4.png" class="img-max-progress">';
}
elseif($name== true && $bio == true && $address == true && $phone == true) {
echo '<img src="../images/progress5.png" class="img-max-progress">';
}
elseif($name== true && $bio == true && $address == true && $phone == true && $ec == true) {
echo '<img src="../images/progress6.png" class="img-max-progress">';
}
elseif($name== true && $bio == true && $address == true && $phone == true && $ec == true && $ecp == true) {
echo '<img src="../images/progress7.png" class="img-max-progress">';
}
elseif($name== true && $bio == true && $address == true && $phone == true && $ec == true && $ecp == true && $bs == true) {
echo '<img src="../images/progress8.png" class="img-max-progress">';
}
if($name != true) {
echo '<img src="../images/progress1.png" class="img-max-progress">';
}
You maybe want to change the == true to what #vlzvl says (check if a variable is empty or not)
Related
When doing php validation should I First do the filter sanitizing/validating or is it okay to do it as part of an if statement see the examples below
First Example
$vvalidation = 0;
if (isset($_POST['db9_name']) && $_POST['db9_name'] != ''){
$name = $_POST['db9_name'];
if (filter_var($name, FILTER_SANITIZE_STRING === null)){
$vvalidation++;
}
} else{
$vvalidation++;
}
Second Example
$vvalidation = 0;
if (isset($_POST['db9_name']) && $_POST['db9_name'] != ''){
$name = $_POST['db9_name'];
$vname = filter_var($name, FILTER_SANITIZE_STRING);
if ($vname === null)){
$vvalidation++;
}
} else{
$vvalidation++;
}
and for email ?
example 1
if (isset($_POST['txtemail']) && $_POST['txtemail'] !== '') {
$vEmail = strtolower(strip_tags(trim($_POST['txtemail'])));
$vEmail = str_replace(' ', '', $vEmail);
if (filter_var($vEmail, FILTER_SANITIZE_EMAIL) === null) {
$vValidation++;
} elseif (filter_var($vEmail, FILTER_VALIDATE_EMAIL) === null) {
$vValidation++;
}
} else {
$vValidation++;
}
example 2
if (isset($_POST['txtemail']) && $_POST['txtemail'] !== '') {
$vEmail = strtolower(strip_tags(trim($_POST['txtemail'])));
$vEmail = str_replace(' ', '', $vEmail);
$email = (filter_var($vEmail, FILTER_SANITIZE_EMAIL);
$email .= (filter_var($vEmail, FILTER_VALIDATE_EMAIL);
if (email === null){
$vValidation++;
} else {
$vValidation++;
}
or does it not really matter?
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;
}
}
?>
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
$dir = 'dir';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);
if (strlen($q) < 3) {
echo "Search must be longer than 3 characters.<br><br>";
} else {
if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
$nicefile = str_replace(".mp3", "", "$file");
$info = pathinfo($file);
if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) {
echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>";
echo "<br>";
}elseif(!isset($errorMsg)){
$errorMsg = "ERROR: File not found.<br><br>";
}else{}
}elseif(!isset($errorMsge)){
$errorMsge = "ERROR: File not found.<br><br>";
}else{}
}
if (isset($errorMsge)){echo $errorMsge;}
else{}
if (isset($errorMsg)){echo $errorMsg;}
else{}
}else{echo"ERROR: File not found.<br><br>";}
}
closedir($res);
?>
this is like a search script and the problem is #
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude))
{
it shows the error msg whether there is results or file named like search query or not.
any idea how to fix that?
Im trying to make a function, that takes an input, determines its value, and outputs a word from an already existing array that has been included in the script. The problem is the output is blank, i believe the function ignores variables already in the script, is there a way to change this so existing variables arent ignored by the function?
Here is the function:
The words need to be from an array because of multi-lingual requirements.
function get_genre($id)
{
if($id == "1"){
$genre = $lang['277'];
}
if($id == "2"){
$genre = $lang['278'];
}
if($id == "3"){
$genre = $lang['279'];
}
if($id == "4"){
$genre = $lang['280'];
}
if($id == "5"){
$genre = $lang['281'];
}
if($id == "6"){
$genre = $lang['282'];
}
if($id == "7"){
$genre = $lang['283'];
}
if($id == "8"){
$genre = $lang['284'];
}
if($id == "9"){
$genre = $lang['285'];
}
if($id == "10"){
$genre = $lang['286'];
}
if($id == "11"){
$genre = $lang['287'];
}
if($id == "12"){
$genre = $lang['288'];
}
if($id == "13"){
$genre = $lang['289'];
}
if($id == "14"){
$genre = $lang['290'];
}
if($id == "15"){
$genre = $lang['374'];
}
return $genre;
}
function get_genre($id)
{
global $lang;
....
}
or
function get_genre($id, $lang) //Must pass $lang array to function here
{
}
function get_genre($id)
{
global $lang;
if($id == "1"){
$genre = $lang['277'];
}
if($id == "2"){
$genre = $lang['278'];
}
if($id == "3"){
$genre = $lang['279'];
}
if($id == "4"){
$genre = $lang['280'];
}
if($id == "5"){
$genre = $lang['281'];
}
if($id == "6"){
$genre = $lang['282'];
}
if($id == "7"){
$genre = $lang['283'];
}
if($id == "8"){
$genre = $lang['284'];
}
if($id == "9"){
$genre = $lang['285'];
}
if($id == "10"){
$genre = $lang['286'];
}
if($id == "11"){
$genre = $lang['287'];
}
if($id == "12"){
$genre = $lang['288'];
}
if($id == "13"){
$genre = $lang['289'];
}
if($id == "14"){
$genre = $lang['290'];
}
if($id == "15"){
$genre = $lang['374'];
}
return $genre;
}
While this isn't ideal, have you considered using an associative array?
var $lookupArray = array();
$lookupArray["1"] = $lang['274'];
....
Then you can call it like this:
function get_genre($id)
{
return(array_key_exists($id,$lookupArray)) ? $lookupArray[$id] : null;
}