Jump to content

Get "true" from multiple "if"


xfire123
 Share

Go to solution Solved by ginerjm,

Recommended Posts

Hello. Sorry if my question is not very clear. I have multiple "for" functions. Inside them there are "if"s(maximum 1 if/else in each "for" function).

I want to create function that echo something if one of this "if"s are true. Can you give me an example? Thanks! :)

Edited by xfire123
Link to comment
Share on other sites

                            for($ri = 0; $ri < $numrows; $ri++) {
                            $row2 = pg_fetch_array($result, $ri);
                            if ($row["blok11"] > 1.2){
                            $warning =  "<span class='warning'>{$row['blok11']} l/s</span>";
                            } else {
                            $warning =  "<span class='normal'>{$row['blok11']} l/s</span>";
                            }
                            echo $warning;
                            } 

If i have couple of this, i need:

if ("for" || "for") == true do something...

Edited by xfire123
Link to comment
Share on other sites

your example still doesn't provide enough information about what the problem is. it doesn't show in HOW there are more than one of anything. btw - for(){} is a loop construct, not a function. 

 

the only things your example shows are -

 

1) that's not your real code. there are $row2 and $row variables that don't match.

 

2) you likely have a badly designed database table, with a sequence of name-numbered columns, that should be normalized to a having a separate row for each data item, which is likely what's causing you to need complicated code to produce the output.

 

3) you are repeating code/markup. if the only thing that's different based on the comparison is the class name, that's the only thing that should be produced in the conditional logic, then use the resulting class name in a single line of markup for the output.

Link to comment
Share on other sites

your example still doesn't provide enough information about what the problem is. it doesn't show in HOW there are more than one of anything. btw - for(){} is a loop construct, not a function. 

 

the only things your example shows are -

 

1) that's not your real code. there are $row2 and $row variables that don't match.

 

2) you likely have a badly designed database table, with a sequence of name-numbered columns, that should be normalized to a having a separate row for each data item, which is likely what's causing you to need complicated code to produce the output.

 

3) you are repeating code/markup. if the only thing that's different based on the comparison is the class name, that's the only thing that should be produced in the conditional logic, then use the resulting class name in a single line of markup for the output.

 

1) yes i have mistake there. The $row variable was in another please...

 

2) I have separate columns

 

3) This is code for example, there is some more data in the "if"s

 

 

I will try to explain more:

I have a table with multiple columns. I get every last row of every column. I compare if any data is out of range by using "if" statements. The "for" loop in this case i think is not necessary because i use only one row. This is due to the lack of sufficient knowledge by me :). I use this information to display 5 measures in the page.

Now i want to group them in to one and display an element that will represent text alarm when one of this elements have boolean type of true

Link to comment
Share on other sites

The code is mine. There is not need to test me. I'm in hurry to copy and paste. This is another "if" from other 4 like him. What is the problem... this is simple example what is the structure.

$result = pg_exec($link, "select timestamp,blok11 from otv_fil WHERE blok11 NOT BETWEEN -4 AND 10 ORDER BY timestamp desc limit 1;");
Link to comment
Share on other sites

....This is another "if" from other 4 like him. ....

 

$result = pg_exec($link, "select timestamp,blok11 from otv_fil WHERE blok11 NOT BETWEEN -4 AND 10 ORDER BY timestamp desc limit 1;");

 

Huh? 'This is another "if.....'? Uh, I don't see an if statement here.

 

You say you are in a hurry. Perhaps that is why you are having difficulty understanding what the forum is asking you to provide and why you need so much help with this problem.

 

Maybe you could just explain your scenario without the code so that we can see your problem and help with your solution.

Edited by ginerjm
Link to comment
Share on other sites

Huh? 'This is another "if.....'? Uh, I don't see an if statement here.

Yes in here

                            for($ri = 0; $ri < $numrows; $ri++) {
                            $row2 = pg_fetch_array($result, $ri);
                            if ($row["blok9osx"] > 1.2){
                            $warning =  "<span class='warning'>{$row['blok9osx']} l/s</span>";
                            } else {
                            $warning =  "<span class='normal'>{$row['blok9osx']} l/s</span>";
                            }
                            echo $warning;
                            } 

Sorry for my English.

Edited by xfire123
Link to comment
Share on other sites

Is it that English is not your primary language or that you are in an incessant hurry and aren't thinking clearly when you post your replies.

 

You have been asked for your table structure. Show us.

Yes its not my primary language. 

 

Ok. Here is the table:

-- Table: public.otv_fil

-- DROP TABLE public.otv_fil;

CREATE TABLE public.otv_fil
(
    "timestamp" timestamp without time zone,
    blok9osx double precision,
    timestamp2 timestamp without time zone,
    con integer,
    blok9osy double precision,
    timestamp3 timestamp without time zone,
    con2 integer,
    blok11osx double precision,
    timestamp4 timestamp without time zone,
    con3 integer,
    blok11osy double precision,
    timestamp5 timestamp without time zone,
    con4 integer,
    filblok10 double precision,
    timestamp6 timestamp without time zone,
    con5 integer,
    filblok11 double precision,
    timestamp7 timestamp without time zone,
    con6 integer
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.otv_fil
    OWNER to postgres;

Is this the structure that i must to show?

Link to comment
Share on other sites

Yes that is the "table structure". And as suspected by someone earlier (MacGyver) you have a bad database design. You are putting multiple pairs of similar values into a single record. This is NOT a proper "Normalized" design. You don't put the same data into multiple columns of a table record. Instead you create a table that contains one set of such data in a row and then link to that table from the main table.

 

main table:

key/id/??? varchar(??)

 

values table:

key/id/??? varchar(??),

con integer,

time_added: timestamp,

blok: double precision;

 

Now you have two tables - one with identifying information (just a key for now) and the other with ALL of the related timestamps and values that belong to that key. When you query this you select the rows from both tables which will produce a query result that has only one time/value pair in each record. then you loop thru the results to pick up each value pair one at a time.

Link to comment
Share on other sites

Yes that is the "table structure". And as suspected by someone earlier (MacGyver) you have a bad database design. You are putting multiple pairs of similar values into a single record. This is NOT a proper "Normalized" design. You don't put the same data into multiple columns of a table record. Instead you create a table that contains one set of such data in a row and then link to that table from the main table.

 

main table:

key/id/??? varchar(??)

 

values table:

key/id/??? varchar(??),

con integer,

time_added: timestamp,

blok: double precision;

 

Now you have two tables - one with identifying information (just a key for now) and the other with ALL of the related timestamps and values that belong to that key. When you query this you select the rows from both tables which will produce a query result that has only one time/value pair in each record. then you loop thru the results to pick up each value pair one at a time.

Now can you give me more detail explanation via code example. I don't understand very clear. Maybe you will post a link to tutorial or something?

Link to comment
Share on other sites

Can you give me an example what will be the the code after normalization of the DB.

What will be the for loop after this?

 

This is part of the table with some values. Lets say this is the complete table. How to normalize and make a php loop like my example in the beginning in more simple way like you said?

post-204867-0-78861500-1510043900_thumb.png

Edited by xfire123
Link to comment
Share on other sites

I do hope you understand.

 

People here volunteer their knowledge and time to "help" those trying to learn and not to those put into the position of having to beg for free labor. The business of learning and educating oneself in the pursuit of quality computer solutions is not for the weak of mind or heart. :) It is a highly technical science that takes quite a bit of time and energy to become competent in let alone to master.

Link to comment
Share on other sites

Is there any way to link one "if" statement if is true or false. And then use this condition to another "if"? Like this:

 

ifn(...){...} = true

 

$if1= if1(...){...}

...

$if(n)=ifn(...){...}

 

then if ($if1 || $if(n)) { do something}

 

I hope you understand :)

 

 

I do hope you understand.

People here volunteer their knowledge and time to "help" those trying to learn and not to those put into the position of having to beg for free labor. The business of learning and educating oneself in the pursuit of quality computer solutions is not for the weak of mind or heart.  :) It is a highly technical science that takes quite a bit of time and energy to become competent in let alone to master.

 
ok :)
 
btw i write "I hope you understand" when you write it :D the Matrix is strong
Edited by xfire123
Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.