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...
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!
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
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...
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:
$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.
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:
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...