Using a wildcard, ereg maybe?

Hi, I have the following piece of code which takes everyrow from a mysql query and creates a variable:

while (($row = mysql_fetch_array($result)) != false)
{
      foreach ($row as $key1=>$val1)
      {
                 $$key1 = $val1;  
      } //Closes foreach loop
} //Closes while loop

I have about 30 variables that all belong to a same group and all start with the following characters: clrpgnbr_. What I would like to do, is create an if statement which reads any variable that starts with clrpgnbr_, and if the variable has a value, add one to a counter. I tried this, obviously did not work, just a stab in the dark:

totclrpgnbr=0;
while (($row = mysql_fetch_array($result)) != false)
{
      foreach ($row as $key1=>$val1)
      {
                 $$key1 = $val1;  
      } //Closes foreach loop
                if(ereg("^",$clrpgnbr)!="NULL)
                        totclrpgnbr++;
} //Closes while loop

What is the correct syntax for ereg? I saw a few examples on php.net, but they weren't much help. Thanks!

horaliaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Raynard7Connect With a Mentor Commented:
Hi,

Considering your design I would suggest that you have two tables rather than one.

I would have one table that holds your information that is unique to each row, and have an auto_number associated with it.

I would have another table that is linked on the key fields from the other table and then have one column for the color.

For each column that you currently have I would have a row for those (where it is not null)

This would mean that you would not need to use $$ to assign variables, you could simply put them in an array.

It also means that if you have more than 30 selections or < 30 you can never run out of room.

This would help it be dynamic and make your queries easier.

The problem is that with excel people want relationships between data that is easy to see to the naked eye - this is why excel is not setup as a relational database, it is also why people just add columns when they need to (and what makes automating it such a pain) - you may need to move your data around to get the best fit for your database (ie run 30 select into statements from your current setup to get this looking right, but in the end you will save a lot of time.

You can send me your spreadsheet if you wish.
0
 
Raynard7Commented:
I'd suggest the fastest and easiest way would be to incorporate this into your query.

ie

Select count(*) from tableName where rowColumn like 'clrpgnbr\_%'
0
 
horaliaAuthor Commented:
OK, let me try that, I had been thinking of doing this also but was not sure...
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
horaliaAuthor Commented:
It didn't work. Let me add a little more detail... clrpgnbr_30, clrpgnbr_29, etc., is the name of the column. If there is a value stored in these columns, that is, from clrpgnbr_30 to clrpgnbr_1, I would like to count it.
0
 
Raynard7Commented:
Hi, this does not sound like great table structure, but couldn't you do something like:

$totclrpgnbr = 0;
$prefix = "clrpgnbr_";

while (($row = mysql_fetch_array($result)) != false)
{
     foreach ($row as $key1=>$val1)
     {
               $$key1 = $val1;  
             if(!(is_null($val1)) && substr($key1, 0, strlen($prefix)) === $prefix){
                      $totclpgnbr++;
             }
     } //Closes foreach loop
} //Closes while loop
0
 
horaliaAuthor Commented:
I'll try this suggestion tomorrow Raynard and let you know. About the table structure, I really didn't know how else to design it. This form replaces an Excel spreadsheet form that the users have been using for years now. This particular form is used to record deadlines. (I work for a newspaper). The press has 30 color positions maximum to print out, each position has a deadline that is automatically calculated. The user can enter the page number and the time it ran, that's why I have all these different columns in the table. When designing the form, I was thinking of using sub-forms (I have an Access background), but I never really could bring this concept over to PHP. If you have any suggestions as to the design of this table, I would really appreciate it. Thanks!
0
 
Raynard7Commented:
I'll get back to you shortly about the table design.
0
 
horaliaAuthor Commented:
Thanks, I appreciate that!
0
 
horaliaAuthor Commented:
Raynard, may I send you the spreadsheet that was used for this form to your e-mail address?
0
 
horaliaAuthor Commented:
Thanks Raynard! I'll send you the spreadsheet so you can take a look at it and let me know. I'm also in the middle of another form and on that one, I'm completely stuck. Anyways, let me send you the spreadsheet so you can take a look at it. I really appreciate all of your help!
0
 
Raynard7Commented:
I've received your spreadsheet, will try to get a look at it today.
0
 
horaliaAuthor Commented:
Thanks! I don't work tomorrow, it's Thanksgiving week, so take your time. The biggest issue I have is generating automatic lines in the form, I read somewhere that that could be done using JavaScript, but I haven't looked into that yet.
0
 
horaliaAuthor Commented:
I followed your suggestion Raynard and found a tutorial on the web with examples that detail what you explained. My code looks much cleaner now, and so does the database. If you still have some suggestions, I will appreciate that, maybe I can compare what I already have to what you suggest. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.