Solved

Using a wildcard, ereg maybe?

Posted on 2006-11-20
13
364 Views
Last Modified: 2008-02-26
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!

0
Comment
Question by:horalia
  • 8
  • 5
13 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17984329
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
 

Author Comment

by:horalia
ID: 17984348
OK, let me try that, I had been thinking of doing this also but was not sure...
0
 

Author Comment

by:horalia
ID: 17984367
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
 
LVL 35

Expert Comment

by:Raynard7
ID: 17984571
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
 

Author Comment

by:horalia
ID: 17984657
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
 
LVL 35

Expert Comment

by:Raynard7
ID: 17984882
I'll get back to you shortly about the table design.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:horalia
ID: 17988335
Thanks, I appreciate that!
0
 

Author Comment

by:horalia
ID: 17988347
Raynard, may I send you the spreadsheet that was used for this form to your e-mail address?
0
 
LVL 35

Accepted Solution

by:
Raynard7 earned 500 total points
ID: 17990673
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
 

Author Comment

by:horalia
ID: 17996889
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
 
LVL 35

Expert Comment

by:Raynard7
ID: 17999108
I've received your spreadsheet, will try to get a look at it today.
0
 

Author Comment

by:horalia
ID: 17999330
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
 

Author Comment

by:horalia
ID: 18183855
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now