xfire123 Posted November 6, 2017 Share Posted November 6, 2017 (edited) 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 November 6, 2017 by xfire123 Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 6, 2017 Share Posted November 6, 2017 How about you give US an example of what you already have. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 6, 2017 Author Share Posted November 6, 2017 (edited) 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 November 6, 2017 by xfire123 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 6, 2017 Share Posted November 6, 2017 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. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 6, 2017 Author Share Posted November 6, 2017 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 6, 2017 Share Posted November 6, 2017 Whatis your db table structure and the query/processing you are using to get that data? Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 6, 2017 Author Share Posted November 6, 2017 $result = pg_exec($link, "select timestamp,blok9osx from otv_fil WHERE blok9osx NOT BETWEEN -4 AND 10 ORDER BY timestamp desc limit 1;"); One column with type - "temestamp" and five with type - "double precision" Quote Link to comment Share on other sites More sharing options...
Barand Posted November 6, 2017 Share Posted November 6, 2017 That query doesn't match the column names in your code. Sorry to have wasted my time. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 6, 2017 Author Share Posted November 6, 2017 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;"); Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 6, 2017 Share Posted November 6, 2017 To quote you: Â I get every last row of every column. Â What the heck does that mean? The last 'row' of a 'column'? Do you know what you are talking about? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 6, 2017 Share Posted November 6, 2017 (edited) ....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 November 6, 2017 by ginerjm Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 6, 2017 Author Share Posted November 6, 2017 (edited) To quote you: Â I get every last row of every column. Â What the heck does that mean? The last 'row' of a 'column'? Do you know what you are talking about? Â Â I meant the last record in the table. Edited November 6, 2017 by xfire123 Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 6, 2017 Author Share Posted November 6, 2017 (edited) 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 November 6, 2017 by xfire123 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 6, 2017 Share Posted November 6, 2017 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. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 6, 2017 Author Share Posted November 6, 2017 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? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 6, 2017 Share Posted November 6, 2017 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. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 7, 2017 Author Share Posted November 7, 2017 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? Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 7, 2017 Author Share Posted November 7, 2017 Btw i auto import the data in table from csv files in particular time. Just mention. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 7, 2017 Author Share Posted November 7, 2017 Im reading tutorials atm. Thanks. I will ask again when i'm ready. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 7, 2017 Author Share Posted November 7, 2017 (edited) 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? Edited November 7, 2017 by xfire123 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 7, 2017 Share Posted November 7, 2017 When you fully understand proper database design you will then learn how to write a proper query and to then use the data from your db. No - I will not write it for you. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 7, 2017 Author Share Posted November 7, 2017 When you fully understand proper database design you will then learn how to write a proper query and to then use the data from your db. No - I will not write it for you. Ok thx for help. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 7, 2017 Share Posted November 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
xfire123 Posted November 7, 2017 Author Share Posted November 7, 2017 (edited) 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 the Matrix is strong Edited November 7, 2017 by xfire123 Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted November 7, 2017 Solution Share Posted November 7, 2017 You should do a little reading on the use of the if/else statement. Any php reference can help you understand how they are used. Especially the official PHP manual at php.net Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.