Jnerocorp Posted April 24, 2011 Share Posted April 24, 2011 Hello, Example X = 50 I have been trying to find a way so that I can loop a foreach to insert into a database X and every other number below X unless X already exists. is this possible? -John Link to comment https://forums.phpfreaks.com/topic/234604-help-foreach-and-all-numbers-less-then-x/ Share on other sites More sharing options...
Jnerocorp Posted April 24, 2011 Author Share Posted April 24, 2011 ok let me clarify this: say i submit 50 X will = 50 if 50 does not exist in database -> add it then 49 48 47 (already exists) so doesnt need to be added -> moves onto next 46 45 44(already exists) so doesnt need to be added -> moves onto next 43 || 3 2 1 Link to comment https://forums.phpfreaks.com/topic/234604-help-foreach-and-all-numbers-less-then-x/#findComment-1205657 Share on other sites More sharing options...
wildteen88 Posted April 24, 2011 Share Posted April 24, 2011 Why are you needing to add the numbers 50 (or whatever X is) all the way down to 1 to your database? Link to comment https://forums.phpfreaks.com/topic/234604-help-foreach-and-all-numbers-less-then-x/#findComment-1205658 Share on other sites More sharing options...
Jnerocorp Posted April 24, 2011 Author Share Posted April 24, 2011 well my site is a review site for episodes of anime shows the reviews are written by the users and some shows have up to 300 episodes i dont want to have to add each episode in individually so i want it to populate the database with every episode number before X except for episodes that already exists so i can correctly load pages based on episode id and the user can submit reviews. Link to comment https://forums.phpfreaks.com/topic/234604-help-foreach-and-all-numbers-less-then-x/#findComment-1205660 Share on other sites More sharing options...
Fadion Posted April 24, 2011 Share Posted April 24, 2011 Let's assume you have a table like this: [pre]Table "shows" ---------------------- id | title | episode 1 | Naruto | 1 2 | Naruto | 5 3 | Naruto | 400[/pre] So you want to add shows in the table to fill in the episode gaps? If that's the case, the code below should help. <?php $show_title = 'Naruto'; $num_episodes = 400; for ($i=1; $i <= $num_episodes; $i++) { $results = mysql_query("SELECT id FROM shows WHERE title='$show_title' AND episode=$i LIMIT 1"); if (!mysql_num_rows($results)) { $results = mysql_query("INSERT INTO shows (title, episode) VALUES ('$show_title', '$i')"); } } ?> I have wrote a simple for() loop that starts from 1 and goes up to the last episode specified (in that case 400). The first query checks if there is any episode of the show with the current iteration of $i; if not, it will add a new row with the new episode. Pretty simple! Hope it's what you were looking for. Link to comment https://forums.phpfreaks.com/topic/234604-help-foreach-and-all-numbers-less-then-x/#findComment-1205684 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 Instead of running a query in a loop, build the query string in a loop, and execute the query once. $existing = array(); $query = "SELECT episode_number FROM shows_table WHERE show_title = '$title_id'"; $result = mysql_query( $query ); while( $array = mysql_ftech_row($result) ) { $existing[] = $array[0]; } $add_record = array(); $max_episode_number = 150; for( $i = 1; $i <= $max_episode_number; $i++ ) { if( !in_array($i, $existing) ) { $add_record[] = "$title_id, $i"; } } $query = "INSERT INTO shows_table ( title_id, episode_number ) VALUES ( " . implode( '), (', $add_record) . " )"; $result = $mysql_query($query); Link to comment https://forums.phpfreaks.com/topic/234604-help-foreach-and-all-numbers-less-then-x/#findComment-1205753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.