Help a noob with some code

Hello all

I’m very new to PHP and trying to learn.

So to give some context, i started dabling in a website code to try and make it do more stuff than it does atm.

So the original is as it follows, all working fine :slight_smile:

$NewTID = mysql_insert_id ();

// BEGIN AUTO LINKING
        // NOTE Category IDs are: TV-HD = 47; TV-SD = 16; Soaps = 54;

        // get the current day (12 hour behind current time)
        $plugin_shows_airdate = date('l', strtotime('-12 hour'));

        // query possible shows to link for today
        $plugin_shows_query = sql_query ("SELECT * FROM plugin_shows WHERE airDate = '$plugin_shows_airdate' AND (linkSD = '' OR linkHD = '')");

        // check we got some results
        if (mysql_num_rows ($plugin_shows_query) > 0)
        {
            // so far we mark that we have not matched a show
            $plugin_shows_unmatched = 1;

            // until we match a show, loop through results
            while ($plugin_shows_unmatched == 1 && $plugin_shows_squery=mysql_fetch_assoc($plugin_shows_query))
            {
                // if we have a TV-SD cat
                if ($category == '16')
                {
                    // look for regex match
                    if ( preg_match ( '/'.$plugin_shows_squery['sdRegex'].'/', $name, $plugin_shows_matches)) 
                    {
                        // check its not already linked
                        if (empty($plugin_shows_squery['linkSD'])) 
                        {
                            // store the result
                            $plugin_shows_uquery = sql_query ("UPDATE plugin_shows SET linkSD = 'https://www.test.eu/details.php?id=".$NewTID."' WHERE id = '".$plugin_shows_squery['id']."'") ;
                        }
                        // skip further sub-queries
                        $unmatched = 0;
                    }
                }
                // or if we have a TV-HD cat
                elseif($category == '47')
                {
                    // look for regex match
                    if ( preg_match ( '/'.$plugin_shows_squery['hdRegex'].'/', $name, $plugin_shows_matches) )
                    {
                        // check its not already linked
                        if (empty($plugin_shows_squery['linkHD']))
                        {
                            // store the result
                            $plugin_shows_uquery = sql_query ("UPDATE plugin_shows SET linkHD = 'https://www.test.eu/details.php?id=".$NewTID."' WHERE id = '".$plugin_shows_squery['id']."'");
                        }
			// skip further sub-queries
			$unmatched = 0;
                    }
                }
            }
        }
        // END AUTO LINKING
	}

So i started changing it a bit to make it do some more stuff and changed it into

$NewTID = mysql_insert_id ();

        // BEGIN AUTO LINKING
        // NOTE Category IDs are: TV-HD = 47; TV-SD = 16; Soaps = 54;

        // get the current day (12 hour behind current time)
        $plugin_shows_airdate = date('l', strtotime('-12 hour'));

        // query possible shows to link for today
        $plugin_shows_query = sql_query ("SELECT * FROM plugin_shows WHERE airDate = '$plugin_shows_airdate' AND (linkSD = '' OR linkHD = '')");

        // check we got some results
        if (mysql_num_rows ($plugin_shows_query) > 0)
        {
            // so far we mark that we have not matched a show
            $plugin_shows_unmatched = 1;

            // until we match a show, loop through results
            while ($plugin_shows_unmatched == 1 && $plugin_shows_squery=mysql_fetch_assoc($plugin_shows_query))
            {
                // if we have a TV-SD cat
                if ($category == '16' || $category == '54')
                {
                    // look for regex match
                    if ( preg_match ( '/'.$plugin_shows_squery['sdRegex'].'/', $name, $plugin_shows_matches) ) 
                    {
                        // check its not already linked
                        if (empty($plugin_shows_squery['linkSD'])) 
                        {
                            // store the result
                            $plugin_shows_uquery = sql_query ("UPDATE plugin_shows SET linkSD = 'https://www.test.eu/details.php?id=".$NewTID."' WHERE id = '".$plugin_shows_squery['id']."'");
                        }
                        // skip further sub-queries
                        $unmatched = 0;
                    }
                }
                // or if we have a TV-HD cat
                elseif($category == '47' || $category == '54')
                {
                    // look for regex match
                    if ( preg_match ( '/'.$plugin_shows_squery['hdRegex'].'/', $name, $plugin_shows_matches) )
                    {
                        // check its not already linked
                        if (empty($plugin_shows_squery['linkHD']))
                        {
                            // store the result
                            $plugin_shows_uquery = sql_query ("UPDATE plugin_shows SET linkHD = 'https://www.test.eu/details.php?id=".$NewTID."' WHERE id = '".$plugin_shows_squery['id']."'");
                        }
                    }
                }
            }
        }
 	  // get the current day (24 hour behind current time)
        $xsplugin_shows_airdate = date('l', strtotime('-24 hour'));

        // query possible shows to link for today
        $plugin_shows_query = sql_query ("SELECT * FROM plugin_shows WHERE airDate = '$plugin_shows_airdate' AND (linkSD = '' OR linkHD = '')");

        // check we got some results
        if (mysql_num_rows ($plugin_shows_query) > 0)
        {
            // so far we mark that we have not matched a show
            $plugin_shows_unmatched = 1;

            // until we match a show, loop through results
            while ($plugin_shows_unmatched == 1 && $plugin_shows_squery=mysql_fetch_assoc($plugin_shows_query))
            {
                 // if we have a TV-SD cat
                if ($category == '16' || $category == '54')
                {
                    // look for regex match
                    if ( preg_match ( '/'.$plugin_shows_squery['sdRegex'].'/', $name, $plugin_shows_matches))
                    {
                        // check its not already linked
                        if (empty($plugin_shows_squery['linkSD']))
                        {
                            // store the result
                            $plugin_shows_uquery = sql_query ("UPDATE plugin_shows SET linkSD = 'https://www.test.eu/details.php?id=".$NewTID."' WHERE id = '".$plugin_shows_squery['id']."'");
                        }
                        // skip further sub-queries
                        $unmatched = 0;
                    }
                }
                // or if we have a TV-HD cat
                elseif($category == '47' || $category == '54')
                {
                    // look for regex match
                    if ( preg_match ( '/'.$plugin_shows_squery['hdRegex'].'/', $name, $plugin_shows_matches) )
                    {
                        // check its not already linked
                        if (empty($plugin_shows_squery['linkHD']))
                        {
                            // store the result
                            $plugin_shows_uquery = sql_query ("UPDATE plugin_shows SET linkHD = 'https://www.test.eu/details.php?id=".$NewTID."' WHERE id = '".$plugin_shows_squery['id']."'");
                        }
			// skip further sub-queries
			$unmatched = 0;
                    }
                }
            }
        }
        // END AUTO LINKING
	}

After these changes, everything worked as intended … except it wasn’t picking up the TV-HD cat for $category == ‘54’ but that isn’t problematic even tho i don’t know how to make it work.
Plus i wanted to add a 48h behind current time, so i did what i did previously and added the same code with the 48 less change and it stoped working… the page just gives a 500 error :frowning:

My knowledge of php is too low to solve this, so i’m asking for help.

How can i fixe the TV-HD cat to pick up the category 54 and the 48 less change?

Thanks alot all

Cheers

If you want to include category 54 in your checks, use

if ($category === '16' && $category === '54')

and

elseif ($category === '47' && $category === '54')

Just remember that if one of the categories is not 54, both checks will fail.

For details on comparison operators, see the PHP manual.

To add 48h behind current time, use '-48 hour'.

Regarding the 500 error, you should have error reporting on while testing. Place the following three lines at the top of your php file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Now, whenever you run your code and it throws an error, that code should tell you exactly what caused it.

Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service