Solved

Using a wildcard, ereg maybe?

Posted on 2006-11-20
13
367 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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