Jump to content

[SOLVED] Is it possible to base a function off of a value from MySQL DB?


tfburges

Recommended Posts

Long story short...

 

Can I do this?

 

if (mysql_result($result_section,$cntr) == ('Machine Info.' or 'Machine Information' or 'Machine Info')) {
    echo "Etc.";
}

 

 

 

 

 

 

 

 

 

Long version...

 

Suppose a table looks like this:

+--------------------------------+----------------+------------------------+
| report_name                    | section        | field_desc             |
+--------------------------------+----------------+------------------------+
| Air Dryer Operators Log        | Customer Info. | Company Name           |
| Air Dryer Operators Log        | Customer Info. | City                   |
| Air Dryer Operators Log        | Customer Info. | Address Line 2         |
| Air Dryer Operators Log        | Customer Info. | Address Line 1         |
| Air Dryer Operators Log        | Customer Info. | Company Name           |
| Air Dryer Operators Log        | Customer Info. | City                   |
| Air Dryer Operators Log        | Customer Info. | Address Line 2         |
| Air Dryer Operators Log        | Customer Info. | Address Line 1         |
| Air Dryer Operators Log        | Machine Info.  | Type of Inspection     |
| Air Dryer Operators Log        | Machine Info.  | Plant ID No.           |
| Air Dryer Operators Log        | Measurements   | Inlet Air Temperature  |
| Air Dryer Operators Log        | Measurements   | Inlet Air Temperature  |
| Air Dryer Operators Log        | Measurements   | Inlet Air Pressure     |
| Air Dryer Operators Log        | Measurements   | Inlet Air Pressure     |
| Air Dryer Operators Log        | Measurements   | Outlet Air Pressure    |
| Air Dryer Operators Log        | Ratings        | Amps                   |
| Air Dryer Operators Log        | Ratings        | Voltage                |
| Air Dryer Operators Log        | Ratings        | Voltage                |
| Air Dryer Operators Log        | Ratings        | Amps                   |
| Air Dryer Operators Log        | Ratings        | Replacement Element    |
| Inspection Report for Air Care | Customer Info. | State                  |
| Inspection Report for Air Care | Customer Info. | State                  |
| Inspection Report for Air Care | Machine Info.  | Plant ID No.           |
| Inspection Report for Air Care | Machine Info.  | Type of Inspection     |
| Inspection Report for Air Care | Measurements   | Outlet Air Pressure    |
| Inspection Report for Air Care | Measurements   | Outlet Air Temperature |
| Inspection Report for Air Care | Measurements   | Outlet Air Temperature |
| Inspection Report for Air Care | Ratings        | Replacement Element    |
+--------------------------------+----------------+------------------------+

(There are actually many more columns beyond field_desc but you get the point.)

 

I'm trying to split the report_names into sections, each with their respective subsections so that it looks something like...

 

-Air Dryer Operator's Log
--Machine Info.
---Dryer Serial No.
---Dryer Model No.
---Etc.
--Customer Info.
---Customer Name
---Customer Address
---Etc.
--Ratings
---Etc.
-Inspection Report for Air Care
--Machine Info.
---Etc.
--Customer Info.
---Etc.
--Visual
---Etc.

 

 

I've been trying to use the following code to do it:

for ($cntr = 0; $cntr < $num_rows; $cntr += 1) {
  if (mysql_result($result_report_name,$cntr) == $selected_report) {  
   if (mysql_result($result_section,$cntr) == ('Machine Info.' or 'Machine Information' or 'Machine Info')) {
    echo "
       <tr><td width=225 align=right valign=middle>",mysql_result($result_field_desc, $cntr),": </td><td width=225 align=left valign=middle><input name=",mysql_result($result_input_name, $cntr)," type=",mysql_result($result_field_type, $cntr),"> ",mysql_result($result_unit_type, $cntr),"</td></tr>
    ";
   }
  }
}

 

And I also wrote the following code that might help me single out where each report_name and section_name change.

for ($cntr = 1; $cntr < $num_rows; $cntr += 1) {
  if (mysql_result($result_report_name,$cntr) !== $length_gitr) {
   $loop_lengths[$arr_length] = $cntr-1;
   $length_gitr[$arr_length] = mysql_result($result_report_name,$cntr);
   $arr_length++;
  }
}

Link to comment
Share on other sites

Yes, you can selectively search the database for a particular field have more than one possible value ... select ... where fieldname IN('abc','def','ghi') ... etc.

 

Looking at your specific question, you have a system that appears to allow users to enter multiple variants of the same information. It would be a better solution to allow that sort of data entry ONLY through a drop-down so you can control the user input and avoid personal variants.

Link to comment
Share on other sites

Thanks for the quick reply!

 

The information is actually going to be entered into the database through either a drop-down menu or a text field right next to it, in which case, whatever is entered into the text field gets added to the drop-down menu.  I have my reasons for setting it up like this.

 

So I wouldn't need the IF statement and just the SELECT ... IN statement, right?  Man, I feel like a noob.  I read all through that part of the manual and didn't pick up on that...

Link to comment
Share on other sites

I read all through that part of the manual and didn't pick up on that...

 

Haha, funny you say that. That's why I always have PHP.net and MySQL.com bookmarked! Plus I usually have a couple of tabs open for each. Just keep at it, once you use it once you'll usually get the hang of it and only use the documentation for reference. But hey! That's what it's there for! Right?!

Link to comment
Share on other sites

Haha yup...

 

It would really help me if I knew the absolute limitations of both PHP and MySQL.  I'm not aware of what all has been automated and what all I would have to code by hand.  I'm finding out though that chances are... if it's a relatively simple loop of some kind, there's probably already a command for it.

Link to comment
Share on other sites

Well that's the fun and beauty of programming. The limitations are pretty much endless. There's usually always a way around something. Whether it be the most efficient method or not, it's still a method! Then when you learn more, you can just come back and refine it by perhaps using some of the new techniques you've learnt.

 

Programmers are never happy with their code, they will always come back and change parts of it, however small. Except when the boss is telling your a** to get it released.

Link to comment
Share on other sites

Haha yeah I have a feeling my boss is gonna be on me pretty soon!  It's taking me a bit longer than I anticipated to figure out the syntax on a lot of this stuff.

 

 

 

Also, I have another (hopefully) quick question of which I hope you guys don't mind helping me with...

 

In regards to the drop-down menu where the report and section names can be selected, notice how the list is comprised of a number of report names and section names that are the same.  I want to pull out each name only once where they differ and use that to populate the drop-down menus.  To further clarify, suppose the report_name column looks like:

Air Dryer
Air Dryer
Air Dryer
Air Dryer
Air Dryer
Air Care
Air Care
Air Care
Air Care
Air Dryer
Air Dryer
Air Dryer
Air Dryer
Air Care
Air Care
Air Care
Inspection
Inspection
Air Care

 

Then the pull down menu will have the following options:

Air Dryer
Air Care
Inspection

 

 

All I need is the syntax or a better opinion on how to single these out.  I was going to try to use a for loop but experience tells me there's a better way, probably a simple command.  Populating the drop-down menu's select options isn't at all an issue.  I'll keep reading and looking for a command somewhere.  I really appreciate the help!!

 

 

 

EDIT:  I think the best way to go about this would be to create 2 new tables (report_list and section_list) and post into it any new values (that are entered into the text box next to the drop-down menu) with a "replace into" statement, so that it only adds values that have not yet been added; then retrieve that list from said table to populate the menu.  Any objections as to why this won't work?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.