Marx Posted January 7, 2014 Share Posted January 7, 2014 (edited) <?php $constructors = array ('Renault', 'Citroen', 'Ferrari', 'Fiat', 'Seat', 'Honda', 'Toyota', 'Hyundai'); for ($u=0; $u< count ($constructors); $u++) { if ($u % 2 == 0) { echo 'Pair results:'. $constructors[$u].' '; echo '<br />'; } else { echo 'Odd results:'. $constructors[$u]; echo '<br />'; } }?> I have to insert the Pair result in one of the SQL Tables (Constructor1) and the Odd results in another of the tables (Constructor2) but as they are inside and if and else, i don't know how to do it. I tried with function but i noticed you can't use functions there. Any idea? Example: $query = "INSERT INTO constructors (Constructor1, Constructor2) VALUES ('$constructors[$u]', '$constructors[$u]')"; Thanks for your help. Edited January 7, 2014 by Marx Quote Link to comment https://forums.phpfreaks.com/topic/285167-sql-query-inside-ifelse-statements/ Share on other sites More sharing options...
Ch0cu3r Posted January 7, 2014 Share Posted January 7, 2014 (edited) You can run queries just fine within if/else statements. What have you tried? What doesnt make sense if your query will add duplicate data to your table Example: $query = "INSERT INTO constructors (Constructor1, Constructor2) VALUES ('$constructors[$u]', '$constructors[$u]')"; // ^------ duplicated -----^ Edited January 7, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285167-sql-query-inside-ifelse-statements/#findComment-1464249 Share on other sites More sharing options...
Marx Posted January 7, 2014 Author Share Posted January 7, 2014 (edited) They are not duplicated because it should have been: Example: $query = "INSERT INTO constructors (Constructor1, Constructor2) VALUES ('$constructors[0]', '$constructors[1]')"; and so on, that the part of the if/else statements. So i can get the pair and odd positions in the array. Obviously it doesn't make sense because it was just an example. I tried to do a concatenated query that starts in the if and ends in the else but i'm not sure if that possible because i keep getting mysql errors. Edited January 7, 2014 by Marx Quote Link to comment https://forums.phpfreaks.com/topic/285167-sql-query-inside-ifelse-statements/#findComment-1464251 Share on other sites More sharing options...
Solution cyberRobot Posted January 7, 2014 Solution Share Posted January 7, 2014 Since it sounds like you're processing the $constructors array in groups of two, have you considered using array_chunk()? http://www.php.net/manual/en/function.array-chunk.php Quote Link to comment https://forums.phpfreaks.com/topic/285167-sql-query-inside-ifelse-statements/#findComment-1464252 Share on other sites More sharing options...
Marx Posted January 7, 2014 Author Share Posted January 7, 2014 (edited) Good idea. Thanks cyberRobot & Ch0cu3r. Edited January 7, 2014 by Marx Quote Link to comment https://forums.phpfreaks.com/topic/285167-sql-query-inside-ifelse-statements/#findComment-1464254 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.