Loop to check whether variable isset or not

Discussion in 'PHP' started by LazyD, May 1, 2007.

  1. #1
    So, currently, im working on an application, there are 19 certifications a person can have, they were very specific in wanting each certification to have its own column in the DB table....

    Anyway, im trying to make a loop that will check whether the column is empty, if it is, then it wont echo it onto the screen, and if it is, well you can guess what happens...

    The columns are named like cName1, cName2, cName3 and so forth

    Anyway, my loop looks this, but I cant get it to work...

    $issetnum = 1;
    											
    while($issetnum != 20) {
    if(isset($cName.$issetnum) && !empty($cName.$issetnum)) {
    echo " <li>".$cName.$issetnum." - Expires: ".$cNameExp.$issetnum."</li>";
    }
    $issetnum++;
    }
    PHP:
    Essentially I am trying to make it so I can put the word cName and have it automatically put the number in $issetnum onto the end and loop through, however, im having absolutely no luck...
     
    LazyD, May 1, 2007 IP
  2. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I think $cName.$issetnum is conceptually wrong: if you mean to use a variable variable you should use something like

    $fullname=$cName.$issetnum;
    and then use $$fullname instead of $cName.$issetnum in your above code!
     
    Kendothpro, May 1, 2007 IP
  3. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #3
    also using a <= instead of != in the while is kind of more meaningful, you really want it to be less than 20, not just different
     
    Kendothpro, May 1, 2007 IP
  4. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you, I tried that before actually, using this:

    $checkVar = "cName".$issetnum;

    however, if you try to check if $checkVar is set, it is, because its contents are equal to "cName".$issetnum;

    I figured there had to be a way to do...
     
    LazyD, May 1, 2007 IP
  5. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Oh I just noticed you left the increment OUT of the while
    So basically it is never incremented!
    try this

    $issetnum = 1;
                                               
    while($issetnum < 20) {
    	if(isset($cName.$issetnum) && !empty($cName.$issetnum)) {
    	echo " <li>".$cName.$issetnum." - Expires: ".$cNameExp.$issetnum."</li>";
    	$issetnum++;
    }
    PHP:
     
    Kendothpro, May 1, 2007 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    $cName.$issetnum will give you the contents of the variable $cName followed by the contents of the variable $issetnum

    ${'cName'.$issetnum} will give you the contents of the variable which has the name beginning with cName followed by whatever the value of $issetnum is.
     
    rodney88, May 1, 2007 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Just to simplify it:

    
    
                                              
    for($issetnum = 1;$issetnum <= 20; $issetnum++) {
    $currentNum = 'cName'.$issetnum;
    if(isset(${$currentNum}) && !empty(${$currentNum})) {
    echo " <li>".${$currentNum}." - Expires: ".${$currentNum}."</li>";
    }
    
    }
    
    
    PHP:
     
    jestep, May 1, 2007 IP
  8. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you all for your help, this is just what I needed considering I have another project that I was having this same issue with...
     
    LazyD, May 1, 2007 IP