0

I need the following, this is my function:

function i_iframe( $cadena ) {
   $x = @mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
   while ( $i = mysql_fetch_array( $x ) ) {
      $option = adios( $i['idioma'] );
      echo "<li>".$option."</li>";
   }
   @mysql_free_result( $x );
}

this code outputs:

<li>option name 1</li>
<li>option name 2</li>
<li>option name 3</li>

I need the outputs should be like this:

<li class="selected">option name 1</li> 
<li>option name 2</li>
<li>option name 3</li>

the first result from thee loop should print <li class="selected"> and rest should be <li>

1

2 Answers 2

1
function i_iframe($cadena) {
$x=@mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
bool $first = true;
while($i=mysql_fetch_array($x)) {
    $option = adios($i['idioma']);
    if($first)
    {
        echo "<li class=\"selected\">".$option."</li>";
        $first = false;
    } else {
        echo "<li>".$option."</li>";
    }
}
@mysql_free_result($x); 
}

Shoud do the trick

Sign up to request clarification or add additional context in comments.

Comments

1

Call mysql_fetch_array once for your first element:

function i_iframe($cadena){
$x=@mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo '<li class="selected">', $option, '</li>';
while($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo "<li>".$option."</li>";
}
}
@mysql_free_result($x);
}

Now format your code:

function i_iframe($cadena) {
    $x = @mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");

    if($i = mysql_fetch_array($x)) {
        $option = adios($i['idioma']);
        echo '<li class="selected">', $option, '</li>';

        while($i = mysql_fetch_array($x)) {
            $option = adios($i['idioma']);
            echo '<li>', $option, '</li>';
        }
    }

    @mysql_free_result($x);
}

Stop ignoring errors and use clearer names:

function i_iframe($cadena) {
    $query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");

    if($item = mysql_fetch_array($query)) {
        $option = adios($item['idioma']);
        echo '<li class="selected">', $option, '</li>';

        while($item = mysql_fetch_array($query)) {
            $option = adios($item['idioma']);
            echo '<li>', $option, '</li>';
        }
    }

    mysql_free_result($x);
}

Escape your inputs where they’re used, especially if you weren’t already:

function i_iframe($cadena) {
    $cadena = mysql_real_escape_string($cadena);
    $query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");

    if($item = mysql_fetch_array($query)) {
        $option = adios($item['idioma']);
        echo '<li class="selected">', $option, '</li>';

        while($item = mysql_fetch_array($query)) {
            $option = adios($item['idioma']);
            echo '<li>', $option, '</li>';
        }
    }

    mysql_free_result($x);
}

Now stop using that deprecated extension and enjoy a life of PDO:

function i_iframe($cadena) {
    global $db;

    $query = $db->prepare('SELECT idioma FROM videos WHERE id_peli = :cadena LIMIT 3');
    $query->execute([':cadena' => $cadena]);
    $videos = $query->fetchAll(PDO::FETCH_OBJ);

    $first = array_shift($videos);
    echo '<li class="selected">', htmlspecialchars(adios($first->idioma)), '</li>';

    foreach($videos as $video) {
        echo '<li>', htmlspecialchars(adios($first->idioma)), '</li>';
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.