Marx Posted January 7, 2014 Share Posted January 7, 2014 <?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. 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 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 -----^ 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 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. Link to comment https://forums.phpfreaks.com/topic/285167-sql-query-inside-ifelse-statements/#findComment-1464251 Share on other sites More sharing options...
cyberRobot Posted January 7, 2014 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 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 Good idea. Thanks cyberRobot & Ch0cu3r. 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
Archived
This topic is now archived and is closed to further replies.