Programmation d'un forum

  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Avril 1st, 2011, 10:25 am

Pour le moment, j'ai pris place un projet de programme d'un forum comme une expérience d'apprentissage dans les deux, la programmation PHP et le dépannage (depuis theres lié à des problèmes que je courrais partout)...et je suis tombé sur celui que j'ai besoin d'aide.

Im ce n'est pas vraiment un problème dans la mesure où la façon de créer les catégories et les forums par liste de catégories. La seule façon je peux voir qui est fait à l'aide de 2 boucles...un dans l'autre. La première boucle serait pour les catégories et la seconde à l'intérieur de celui-ci, serait pour les forums à l'intérieur des catégories. Y at-il une autre façon, mieux de le faire, ou est-ce la meilleure façon?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Avril 1st, 2011, 10:25 am

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • Avatar de l’utilisateur
  • Inscription: Mai 22, 2004
  • Messages: 3415
  • Loc: Richland, WA
  • Status: Offline

Message Avril 1st, 2011, 11:42 am

Vous voulez dire pour quand vous les afficher?

Par exemple sur cette page: forum /

Je ne vois aucune raison de ne pas utiliser une boucle imbriquée.
#define NULL (::rand() % 2)
  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Avril 1st, 2011, 12:15 pm

Ouais, comme ça. Je ne sais pas si il y avait une meilleure façon déjà là. Merci.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Avril 1st, 2011, 10:20 pm

J'ai un autre problème ici...celui-ci m'a déconcerté bien.

J'ai obtenu le travail SQL, le tableau est généré juste comme je le veux, mais Im obtenir des mises en garde...Je ne sais pas ce qu'ils font.

Les avertissements je reçois (Eh bien, techniquement son «Avertissement» mais il jette deux fois le même...)
Quote:
Attention: Invalid argument fourni pour foreach () in C: \ wamp \ www \ CMS \ includes \ forum. php à la ligne 51

Attention: Invalid argument fourni pour foreach () in C: \ wamp \ www \ CMS \ includes \ forum. php à la ligne 51

Et le PHP est...
PHP Code: [ Select ]
<?php
 
/*
 * forum.php
 *
 * Initiates the functions needed for the forum.
 */
 
class forum {
 
    function forum()
    {
        global $db, $tpl;
       
        // Making sure that the SQL doesn't cache results here...
        $db->set_opts(array('cache_results' => false));
       
        // Checking if there are any categories available
        $sql = $db->build_key_query(array('SELECT'    => '*',
                                          'FROM'      => FORUM_CAT));
 
        if($db->num_rows($sql) == 0)
        {
            $tpl->error = true;
            $tpl->errorMsg = "There are no categories available yet.";
        }
        else
        {
            // Initiating the forums array
            $tpl->forums = array();
       
            // The SQL to retrieve the categories from the database
            $cat_sql = $db->build_key_query(array('SELECT'    => '*',
                                                  'FROM'      => FORUM_CAT));
           
            // Setting the required loop variables
            $inc = 0;
           
            // The loops to get the categories and forums
            foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
            {
                // The category information
                $tpl->cats[$ckey] = $cvalue;
               
                // The SQL to retrieve the forums for the categories from the database
                $forums_sql = $db->build_key_query(array('SELECT'    => '*',
                                                         'FROM'      => FORUM_FORUMS,
                                                         'WHERE'     => 'catID = ' . $cvalue['catID']));
           
                // Looping through the forums and retrieving them
                foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
                {
                    // Setting the forums array
                    $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
                }
               
                // Increasing the loop increment variable
                ++$inc;
            }
        }
    }
}
  1. <?php
  2.  
  3. /*
  4.  * forum.php
  5.  *
  6.  * Initiates the functions needed for the forum.
  7.  */
  8.  
  9. class forum {
  10.  
  11.     function forum()
  12.     {
  13.         global $db, $tpl;
  14.        
  15.         // Making sure that the SQL doesn't cache results here...
  16.         $db->set_opts(array('cache_results' => false));
  17.        
  18.         // Checking if there are any categories available
  19.         $sql = $db->build_key_query(array('SELECT'    => '*',
  20.                                           'FROM'      => FORUM_CAT));
  21.  
  22.         if($db->num_rows($sql) == 0)
  23.         {
  24.             $tpl->error = true;
  25.             $tpl->errorMsg = "There are no categories available yet.";
  26.         }
  27.         else
  28.         {
  29.             // Initiating the forums array
  30.             $tpl->forums = array();
  31.        
  32.             // The SQL to retrieve the categories from the database
  33.             $cat_sql = $db->build_key_query(array('SELECT'    => '*',
  34.                                                   'FROM'      => FORUM_CAT));
  35.            
  36.             // Setting the required loop variables
  37.             $inc = 0;
  38.            
  39.             // The loops to get the categories and forums
  40.             foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
  41.             {
  42.                 // The category information
  43.                 $tpl->cats[$ckey] = $cvalue;
  44.                
  45.                 // The SQL to retrieve the forums for the categories from the database
  46.                 $forums_sql = $db->build_key_query(array('SELECT'    => '*',
  47.                                                          'FROM'      => FORUM_FORUMS,
  48.                                                          'WHERE'     => 'catID = ' . $cvalue['catID']));
  49.            
  50.                 // Looping through the forums and retrieving them
  51.                 foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
  52.                 {
  53.                     // Setting the forums array
  54.                     $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
  55.                 }
  56.                
  57.                 // Increasing the loop increment variable
  58.                 ++$inc;
  59.             }
  60.         }
  61.     }
  62. }

J'ai aussi un sentiment que je pourrais faire cette fonction un peu plus court mais toujours faire la même chose, mais je ne parviens pas à comprendre comment...

Cette fonction me donne le tableau suivant
Code: [ Select ]
Array
(
  [0] => Array
    (
      [catID] => 2
      [catName] => News and Announcements
      [catDescription] => News and Announcements
      [forums] => 1
      [forum] => Array
        (
          [forumID] => 1
          [catID] => 2
          [forumName] => Policies and Procedures
          [forumDescription] => All policies relating to hosting provided, and abuse/support procedures are housed within
          [topics] => 0
          [posts] => 0
          [lastPoster] =>
        )

    )

  [1] => Array
    (
      [catID] => 3
      [catName] => Support Center
      [catDescription] => Support Center
      [forums] => 0
    )

  [2] => Array
    (
      [catID] => 4
      [catName] => On-Topic Discussion
      [catDescription] => On-Topic Discussion
      [forums] => 0
    )

)
  1. Array
  2. (
  3.   [0] => Array
  4.     (
  5.       [catID] => 2
  6.       [catName] => News and Announcements
  7.       [catDescription] => News and Announcements
  8.       [forums] => 1
  9.       [forum] => Array
  10.         (
  11.           [forumID] => 1
  12.           [catID] => 2
  13.           [forumName] => Policies and Procedures
  14.           [forumDescription] => All policies relating to hosting provided, and abuse/support procedures are housed within
  15.           [topics] => 0
  16.           [posts] => 0
  17.           [lastPoster] =>
  18.         )
  19.     )
  20.   [1] => Array
  21.     (
  22.       [catID] => 3
  23.       [catName] => Support Center
  24.       [catDescription] => Support Center
  25.       [forums] => 0
  26.     )
  27.   [2] => Array
  28.     (
  29.       [catID] => 4
  30.       [catName] => On-Topic Discussion
  31.       [catDescription] => On-Topic Discussion
  32.       [forums] => 0
  33.     )
  34. )


[EDIT] Bon, j'ai réussi à résoudre le problème...appearantly s'il n'y a pas de résultats à prendre alors il n'y a évidemment rien à parcourir {SMILIES_PATH} / icon_lol.gif "alt =": lol: "title =" Laughing ">
PHP Code: [ Select ]
<?php
 
/*
 * forum.php
 *
 * Initiates the functions needed for the forum.
 */
 
class forum {
 
    function forum()
    {
        global $db, $tpl;
       
        // Making sure that the SQL doesn't cache results here...
        $db->set_opts(array('cache_results' => false));
       
        // Checking if there are any categories available
        $sql = $db->build_key_query(array('SELECT'    => '*',
                                          'FROM'      => FORUM_CAT));
 
        if($db->num_rows($sql) == 0)
        {
            $tpl->error = true;
            $tpl->errorMsg = "There are no categories available yet.";
        }
        else
        {
            // Initiating the forums array
            $tpl->forums = array();
       
            // The SQL to retrieve the categories from the database
            $cat_sql = $db->build_key_query(array('SELECT'    => '*',
                                                  'FROM'      => FORUM_CAT));
           
            // Setting the required loop variables
            $inc = 0;
           
            // The loops to get the categories and forums
            foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
            {
                // The category information
                $tpl->cats[$ckey] = $cvalue;
               
                // The SQL to retrieve the forums for the categories from the database
                $forums_sql = $db->build_key_query(array('SELECT'    => '*',
                                                         'FROM'      => FORUM_FORUMS,
                                                         'WHERE'     => 'catID = ' . $cvalue['catID']));
           
                // Checking if the following forum has any results
                if($tpl->cats[$ckey]['forums'] > 0)
                {
                    // Looping through the forums and retrieving them
                    foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
                    {
                        // Setting the forums array
                        $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
                    }
                }
               
                // Increasing the loop increment variable
                ++$inc;
            }
        }
    }
}
  1. <?php
  2.  
  3. /*
  4.  * forum.php
  5.  *
  6.  * Initiates the functions needed for the forum.
  7.  */
  8.  
  9. class forum {
  10.  
  11.     function forum()
  12.     {
  13.         global $db, $tpl;
  14.        
  15.         // Making sure that the SQL doesn't cache results here...
  16.         $db->set_opts(array('cache_results' => false));
  17.        
  18.         // Checking if there are any categories available
  19.         $sql = $db->build_key_query(array('SELECT'    => '*',
  20.                                           'FROM'      => FORUM_CAT));
  21.  
  22.         if($db->num_rows($sql) == 0)
  23.         {
  24.             $tpl->error = true;
  25.             $tpl->errorMsg = "There are no categories available yet.";
  26.         }
  27.         else
  28.         {
  29.             // Initiating the forums array
  30.             $tpl->forums = array();
  31.        
  32.             // The SQL to retrieve the categories from the database
  33.             $cat_sql = $db->build_key_query(array('SELECT'    => '*',
  34.                                                   'FROM'      => FORUM_CAT));
  35.            
  36.             // Setting the required loop variables
  37.             $inc = 0;
  38.            
  39.             // The loops to get the categories and forums
  40.             foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
  41.             {
  42.                 // The category information
  43.                 $tpl->cats[$ckey] = $cvalue;
  44.                
  45.                 // The SQL to retrieve the forums for the categories from the database
  46.                 $forums_sql = $db->build_key_query(array('SELECT'    => '*',
  47.                                                          'FROM'      => FORUM_FORUMS,
  48.                                                          'WHERE'     => 'catID = ' . $cvalue['catID']));
  49.            
  50.                 // Checking if the following forum has any results
  51.                 if($tpl->cats[$ckey]['forums'] > 0)
  52.                 {
  53.                     // Looping through the forums and retrieving them
  54.                     foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
  55.                     {
  56.                         // Setting the forums array
  57.                         $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
  58.                     }
  59.                 }
  60.                
  61.                 // Increasing the loop increment variable
  62.                 ++$inc;
  63.             }
  64.         }
  65.     }
  66. }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • Avatar de l’utilisateur
  • Inscription: Déc 20, 2002
  • Messages: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Online

Message Avril 2nd, 2011, 3:19 pm

Bonne prise, avant que je regardé votre résolution que je cherchais plus à des erreurs de syntaxe, mais il ressemble a fini par être une erreur de logique.
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • Rabid Dog
  • Web Master
  • Web Master
  • Avatar de l’utilisateur
  • Inscription: Mai 21, 2004
  • Messages: 3229
  • Loc: South Africa
  • Status: Offline

Message Avril 24th, 2011, 12:46 pm

Quote:
Attention: Invalid argument fourni pour foreach () in C: \ wamp \ www \ CMS \ includes \ forum.php à la ligne 51


jolie auto explanitory ;) Je suis assez certain que le foreach cartes à une fonction qui exige une valeur afin de traiter. Cela aurait conduit à une exception d'argument valide en Java et C #. Notez que c'est un avertissement et non pas une erreur. Il n'est donc pas fatale juste vous faire savoir les arguments que vous avez passé à foreach ne sont pas valides.
Watch me grow

Afficher de l'information

  • Total des messages de ce sujet: 6 messages
  • Utilisateurs parcourant ce forum: Bigwebmaster et 168 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC