Ankündigung

Einklappen
Keine Ankündigung bisher.

"Korrekturlesen von Script"

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • "Korrekturlesen von Script"

    Hi!
    Ich habe eine seiteninterne Suche geschrieben (Vorabversion sichtbar unter Helga Online - Die Schülerzeitung des Helene-Lange-Gymnasiums Fürth). Jetzt wollte ich fragen, ob jemand die Muse hat, sich das ganze mal durchzulesen (ca. 200 Zeilen) und zu bewerten, ggf. Vorschläge zu machen.
    Ich hab's logisch eingerückt und (in Englisch) durchkommentiert.
    Ich bin mir bewusst, dass es nicht der beste Stil ist, ganze Scripte online zu stellen, aber da ich keine Forenregeln gefunden hab, frag ich einfach mal .

    Das Ganze wird eingebunden in eine index.php, die schon den Kontakt zur Datenbank hergestellt hat.

    Gibt's hier einen freundlichen Kollegen, der bereit wäre mir Nachhilfe zu geben^^? (Schreibe mittlerweile seit einigen Jahren, habe aber meine Scripte bisher noch nie von außen kommentieren lassen).

    Vielen Dank schonmal!

  • #2
    PHP-Code:
    <div class="subheadline">
        Artikelsuche
    </div>

    <form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <input type="hidden" name="pageid" value="<?php echo htmlentities($_GET['pageid']);?>" />
        <input type="text" name="q" <?php if(isset($_GET['q'])) echo "value=\"".trim(htmlentities(stripslashes($_GET['q'])))."\"";?> style="width:25%;" /> <button type="submit">Suche</button><br />
        Suche in: <input type="checkbox" name="t" value="1" <?php if(!isset($_GET['q']) OR isset($_GET['t'])) echo 'checked="checked"'?> id="title"/> <label for="title">Titel</label> <input type="checkbox" name="s" value="1"  <?php if(!isset($_GET['q']) OR isset($_GET['s'])) echo 'checked="checked"'?> id="summary"/> <label for="summary">Zusammenfassung</label> <input type="checkbox" name="c" value="1"  <?php if(!isset($_GET['q']) OR isset($_GET['c'])) echo 'checked="checked"'?> label="content"/> <label for="content">Inhalt</label><br />
        Suche beschr&auml;nken auf Artikel von: <select name="a">
        <option value="none">---</option>
        <?php
            $users 
    mysql_query("SELECT ID, firstname, lastname, initials FROM users ORDER BY firstname ASC") OR die(mysql_error());
            
            while(
    $user mysql_fetch_array($users)){
                echo 
    "<option value=\"".$user['ID']."\" ".((isset($_GET['a']) AND $_GET['a']==$user['ID'])?"selected=\"selected\"":"").">".htmlentities($user['firstname'])." ".htmlentities($user['lastname'])." (".htmlentities($user['initials']).")</option>";
            }
        
    ?>
        </select>
    </form>

    <?php

    if(isset($_GET['q']) AND trim($_GET['q'])!==""){
        
    //extract words from query
        
    $query preg_replace('/[^[:alnum:] ]/'""strtolower(trim($_GET['q'])));
        
    $query explode(" "$_GET['q']);
        
        
    //generate WHERE-clause from words
        
    $where_clause "";
        foreach(
    $query as $word){
            
    $where_clause .= "search_words.word LIKE '%".$word."%' OR ";
        }
        
        
    //delete redundand ORs
        
    $where_clause trim($where_clause" OR ");
            
        
    $articles mysql_query("SELECT
                                                                    articles.ID, articles.title, articles.summary, articles.content, articles.date
                                                                FROM
                                                                    articles
                                                                INNER JOIN
                                                                    search_connectors ON  articles.ID = search_connectors.articleid
                                                                                      AND articles.hidden = '0'
                                                                                      
                                                                                      "
    .((isset($_GET['a']) AND is_numeric($_GET['a']))?"
                                                                                      AND articles.by = '"
    .$_GET['a']."'
                                                                                      AND articles.otherby = ''
                                                                                      "
    :"")."
                                                                                      
                                                                                      AND (
                                                                                              search_connectors.title   "
    .(isset($_GET['t'])? "!= 'NULL'":"= 'NULL'")/*if word is not in title column is NULL (this word may exist in another row where title is not NULL, though)*/."
                                                                                           OR search_connectors.summary "
    .(isset($_GET['s'])? "!= 'NULL'":"= 'NULL'")/*if word is not in summary column is NULL (this word may exist in another row where title is not NULL, though)*/."
                                                                                           OR search_connectors.content "
    .(isset($_GET['c'])? "!= 'NULL'":"= 'NULL'")/*if word is not in text column is NULL (this word may exist in another row where title is not NULL, though)*/."
                                                                                          )
                                                                INNER JOIN
                                                                    search_words ON search_connectors.wordid = search_words.ID
                                                                WHERE
                                                                    "
    .$where_clause."") OR die(mysql_error());
        
        
        
        if(
    mysql_num_rows($articles) > 0){//if articles have been found
        
            
    $article_order   = array();
            
    $article_content = array();
            
            
    /* Define new function 'str_ireplace for php4.
               If it already exists (update to php5) it will
                 be ignored and the faster origninal function
                 will be used */
            
    if(!function_exists('str_ireplace')) {
                function 
    str_ireplace($search,$replace,$subject) {
                    
    $search preg_quote($search"/");
                    return 
    preg_replace("/".$search."/i"$replace$subject);
                }
            }
            
            
    //create listing order
            
    while($article mysql_fetch_array($articles)){
                
    $relevance 0;
                
    $occurence 0;
                
                
                if(!isset(
    $article_order[$article['ID']])){
                    
                    
    //increase relevance per hit depending on position and preformat text
                    
    foreach($query as $word){
                        
    //search in text
                        
    if(isset($_GET['c']) AND !(strpos(strtolower($article['content']), strtolower($word)) === false)){
                            
    $relevance += 20*substr_count(strtolower($article['content']), strtolower($word));//increase relevance by 20 per hit
                        
    }
                        
                        
    //search in summary
                        
    if(isset($_GET['s']) AND !(strpos(strtolower($article['summary']), strtolower($word)) === false)){
                            
    $relevance += 200*substr_count(strtolower($article['summary']), strtolower($word));//increase relevance by 200 per hit
                            
                            
    $article['summary'] = str_ireplace($word"<b>".$word."</b>"$article['summary']); //mark search term in bold
                        
    }
                        
                        
    //search in title
                        
    if(isset($_GET['t']) AND !(strpos(strtolower($article['title']), strtolower($word)) === false)){
                            
    $relevance += 600*substr_count(strtolower($article['title']), strtolower($word));//increase relevance by 600 per hit
                            
                            
    $article['title'] = str_ireplace($word"<b>".$word."</b>"$article['title']); //mark search term in bold
                            
                        
    }
                    }
                    
                    
    //make old articles be shown further down than younger articles with same relevance
                    
    $relevance .= $article['date'];
                    
                    
    //array to be sorted
                    
    $article_order  [$article['ID']] = $relevance;
                    
    //array with contents; replace html special charakters and re-replace bold-tags
                    
    $article_content[$article['ID']] = array('title'=>str_replace(array("&lt;b&gt;""&lt;/b&gt;"), array("<b>""</b>"), htmlentities($article['title'])), 'summary'=>str_replace(array("&lt;b&gt;""&lt;/b&gt;"), array("<b>""</b>"), htmlentities($article['summary'])), 'date'=>$article['date']);
                    
                }
            }
            
            
    //sort articleids by relevance
            
    arsort($article_order);
            
    $hitcount count($article_order);
    ?>

    <div style="border:0; border-top:1px solid orange; background-color:#FFCE9D; width:100%; padding: 0.5mm; margin-top:1cm;">
        <span style="float:left; font-size:1.2em;">
            Ergebnisse
        </span>
        <span style="float:right;">
            deiner Suche nach <b><?php echo trim(htmlentities($_GET['q']));?></b>. Zeige Ergebnisse <b><?php if(isset($_GET['showfrom']) AND is_numeric($_GET['showfrom'])){ $until $_GET['showfrom']+15; echo $_GET['showfrom']."-".$until; } else { echo "1-15";}?></b> von <b><?php echo $hitcount;?></b>
        </span><br style="clear:both;" />
    </div>

    Kommentar


    • #3
      Mhm... hat wohl den Umfang einer Mysql-TEXT-Spalte gesprengt

      Hier der Rest. Es folgt die eigentliche Ausgabe der Suchergebnisse.

      PHP-Code:
      <?php
          
      //Echo next and last side buttons
          
      if($hitcount>15){
              if(!isset(
      $_GET['showfrom']) OR $_GET['showfrom']>0){
                  
      $new_from max(array($_GET['showfrom']-150));//if showfrom is less than 15 this will return 0
                  
      echo "<span style=\"float:left;\"><img src=\"/images/weniger.gif\" alt=\"\" /><img src=\"/images/weniger.gif\" alt=\"\" /><a href=\"".$_SERVER['PHP_SELF']."?".preg_replace("/&showfrom=[0-9]+/"""$_SERVER['QUERY_STRING'])."&amp;showfrom=".$new_from."\">Zur&uuml;ck</a></span>";
              }
              
              if(!isset(
      $_GET['showfrom']) OR $_GET['showfrom']+15<$hitcount){
                  
      $new_from $_GET['showfrom']+15;
                  echo 
      "<span style=\"float:right;\"><img src=\"/images/mehr.gif\" alt=\"\" /><img src=\"/images/mehr.gif\" alt=\"\" /><a href=\"".$_SERVER['PHP_SELF']."?".preg_replace("/&showfrom=[0-9]+/"""$_SERVER['QUERY_STRING'])."&amp;showfrom=".$new_from."\">Weiter</a></span>";
              }
          }
          
          echo 
      "<br style=\"clear:both;\"/><br />";

              
      //If user wants to see more results
              
      if(isset($_GET['showfrom']) AND is_numeric($_GET['showfrom'])){
                  
      $from $_GET['showfrom'];
              }else{
                  
      $from 0;
              }
              
      $i 0;
              
              
      //echo results
              
      foreach($article_order as $ID => $relevance){
                  if(
      $i<$from){//if result is on a previous page
                      
      ++$i;
                      continue;
                  }elseif(
      $i>14+$from){//if result is on a following page
                      
      break;
                  }else{
      // echo result
                      
      echo "<big><a href=\"?pageid=10&amp;articleid=".$ID."\">".$article_content[$ID]['title']."</a></big> <small>Datum: ".date("j.n.Y"$article_content[$ID]['date'])." -- Relevanz:".str_replace($article_content[$ID]['date'], ""$relevance)."</small><br />\n".$article_content[$ID]['summary']."<br /><br />\n";
                  }
                  ++
      $i;
              }
              
          
          
      //Echo next and last side buttons
          
      if($hitcount>15){
              if(!isset(
      $_GET['showfrom']) OR $_GET['showfrom']>0){
                  
      $new_from max(array($_GET['showfrom']-150));//if showfrom is less than 15 this will return 0
                  
      echo "<span style=\"float:left;\"><img src=\"/images/weniger.gif\" alt=\"\" /><img src=\"/images/weniger.gif\" alt=\"\" /><a href=\"".$_SERVER['PHP_SELF']."?".preg_replace("/&showfrom=[0-9]+/"""$_SERVER['QUERY_STRING'])."&amp;showfrom=".$new_from."\">Zur&uuml;ck</a></span>";
              }
              
              if(!isset(
      $_GET['showfrom']) OR $_GET['showfrom']+15<$hitcount){
                  
      $new_from $_GET['showfrom']+15;
                  echo 
      "<span style=\"float:right;\"><img src=\"/images/mehr.gif\" alt=\"\" /><img src=\"/images/mehr.gif\" alt=\"\" /><a href=\"".$_SERVER['PHP_SELF']."?".preg_replace("/&showfrom=[0-9]+/"""$_SERVER['QUERY_STRING'])."&amp;showfrom=".$new_from."\">Weiter</a></span>";
              }
          }
          
          echo 
      "<br style=\"clear:both;\"/><br />";
          }else echo 
      "<br /><br /><big><b>Keine Ergebnisse!</b></big>";
          
      }
      ?>

      Kommentar

      Lädt...
      X