Foros del Web » Programando para Internet » PHP »

no comprendo una linea de script

Estas en el tema de no comprendo una linea de script en el foro de PHP en Foros del Web. Hola a todos. No comprendo una linea de este script. Es una linea que no había visto nunca, el uso de interrogante...etc os pongo primero ...
  #1 (permalink)  
Antiguo 04/09/2010, 07:58
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 9 meses
Puntos: 9
no comprendo una linea de script

Hola a todos. No comprendo una linea de este script. Es una linea que no había visto nunca, el uso de interrogante...etc os pongo primero el script y luego la duda:

Código PHP:
<?php
/*****************************
 *  Simple SQL Search Tutorial by Frost
 *  of Slunked.com
 ******************************/

$dbHost 'localhost'// localhost will be used in most cases
// set these to your mysql database username and password.
$dbUser 'searchuser'
$dbPass 'searchpass';
$dbDatabase 'searchdb'// the database you put the table into.
$con mysql_connect($dbHost$dbUser$dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " mysql_error());

// Set up our error check and result check array
$error = array();
$results = array();

// First check if a form was submitted. 
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
   
$searchTerms trim($_GET['search']);
   
$searchTerms strip_tags($searchTerms); // remove any html/javascript.
   
   
if (strlen($searchTerms) < 3) {
      
$error[] = "Search terms must be longer than 3 characters.";
   }else {
      
$searchTermDB mysql_real_escape_string($searchTerms); // prevent sql injection.
   
}
   
   
// If there are no errors, lets get the search going.
   
if (count($error) < 1) {
      
$searchSQL "SELECT sid, sbody, stitle, sdescription FROM simple_search WHERE ";
      
      
// grab the search types.
      
$types = array();
      
$types[] = isset($_GET['body'])?"`sbody` LIKE '%{$searchTermDB}%'":'';
      
$types[] = isset($_GET['title'])?"`stitle` LIKE '%{$searchTermDB}%'":'';
      
$types[] = isset($_GET['desc'])?"`sdescription` LIKE '%{$searchTermDB}%'":'';
      
      
$types array_filter($types"removeEmpty"); // removes any item that was empty (not checked)
      
      
if (count($types) < 1)
         
$types[] = "`sbody` LIKE '%{$searchTermDB}%'"// use the body as a default search if none are checked
      
          
$andOr = isset($_GET['matchall'])?'AND':'OR';
      
$searchSQL .= implode(" {$andOr} "$types) . " ORDER BY `stitle`"// order by title.

      
$searchResult mysql_query($searchSQL) or trigger_error("There was an error.<br/>" mysql_error() . "<br />SQL Was: {$searchSQL}");
      
      if (
mysql_num_rows($searchResult) < 1) {
         
$error[] = "The search term provided {$searchTerms} yielded no results.";
      }else {
         
$results = array(); // the result array
         
$i 1;
         while (
$row mysql_fetch_assoc($searchResult)) {
            
$results[] = "{$i}: {$row['stitle']}<br />{$row['sdescription']}<br />{$row['sbody']}<br /><br />";
            
$i++;
         }
      }
   }
}

function 
removeEmpty($var) {
   return (!empty(
$var)); 
}
?>
<html>
   <title>My Simple Search Form</title>
   <style type="text/css">
      #error {
         color: red;
      }
   </style>
   <body>
      <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" implode("<br />"$error) . "</span><br /><br />":""?>
      <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
         Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''?>" /><br />
         Search In:<br />
         Body: <input type="checkbox" name="body" value="on" <?php echo isset($_GET['body'])?"checked":''?> /> | 
         Title: <input type="checkbox" name="title" value="on" <?php echo isset($_GET['title'])?"checked":''?> /> | 
         Description: <input type="checkbox" name="desc" value="on" <?php echo isset($_GET['desc'])?"checked":''?> /><br />
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''?><br /><br />
         <input type="submit" name="submit" value="Search!" />
      </form>
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" implode(""$results):""?>
   </body>
</html>

La duda es esta línea:
Código PHP:
$andOr = isset($_GET['matchall'])?'AND':'OR'
sobretodo la utilización del interrogante y los dos puntos. Es programación orientada a objetos. Mil gracias
  #2 (permalink)  
Antiguo 04/09/2010, 08:16
Avatar de maycolalvarez
Colaborador
 
Fecha de Ingreso: julio-2008
Ubicación: Caracas
Mensajes: 12.120
Antigüedad: 15 años, 8 meses
Puntos: 1532
Respuesta: no comprendo una linea de script

se le llama operador ternario, su objetivo es evaluar la expresión y devolver el resultado según sea verdadero o falso, es una forma rápida de hacer un if, por ejemplo:

$valoresperado = (expresión_booleana) ? devolver si expresión es true : devolver esto si es false;

es equivalente a:

if(expresión_booleana){
$valoresperado = devolver si expresión es true;
}else{
$valoresperado = devolver esto si es false;
}

para más detalle: http://www.webtaller.com/maletin/art...rnario-php.php
  #3 (permalink)  
Antiguo 04/09/2010, 08:19
 
Fecha de Ingreso: enero-2009
Ubicación: Neiva, Huila
Mensajes: 196
Antigüedad: 15 años, 3 meses
Puntos: 2
Respuesta: no comprendo una linea de script

bueno ya te lo respondieron....
es similar al if que haces en excel.....
  #4 (permalink)  
Antiguo 04/09/2010, 08:30
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 9 meses
Puntos: 9
Sonrisa Respuesta: no comprendo una linea de script

Muchas gracias!
  #5 (permalink)  
Antiguo 04/09/2010, 08:43
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 9 meses
Puntos: 9
Respuesta: no comprendo una linea de script

por cierto, este script es incorrecto verdad?

Etiquetas: linea
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:58.