Jump to content

Help foreach and all numbers less then X


Jnerocorp

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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