Avatar billede daniel_d Nybegynder
13. januar 2006 - 00:03 Der er 5 kommentarer og
1 løsning

Hvordan fjerne jeg resultater fra en løkke som ikke har en titel?

Som spørgsmålet lyder.
Hvordan fjerne jeg resultater fra denne løkke hvis der ikke er nogen titel?
foreach($results as $res)
{
    ?>
    <div class="katalogdiv">
    <b><?php echo $res['Title']; ?></b><br />
    <?php echo $res['Description']; ?>&nbsp;<a href="<?php echo $res['Url']; ?>" style="color: #333333; text-decoration: none; font-size: 10px; font-weight: bold;" rel="nofollow"><?php echo $res['Title']; ?></a>
    </div>
    <?php
    // Did you see where we used the nbr variable above to write the
    // ranking position to the screen?  We need to increase it by one
    // before the next loop.
    $nbr++;
    }
Avatar billede rtfm Nybegynder
13. januar 2006 - 00:48 #1
Så vidt jeg forstår på dit spørgsmål, er det blot at sige:
if (empty($res['Title'])) continue;
Det skal bare være lige under din foreach. På den måde springes denne over, hvis $res['Title'] er tom.
Avatar billede daniel_d Nybegynder
13. januar 2006 - 00:53 #2
jeg vil prøve det af i morgen... er på vej i seng nu.
Men det er hele indholdet af <div> til </div> der skal springes over og så vise næste resultat som har en titel
Avatar billede daniel_d Nybegynder
13. januar 2006 - 10:16 #3
Well... det virker ikke helt efter planen. Den viser kun det første resultat nu uanset hvad.
Avatar billede rednex Nybegynder
13. januar 2006 - 11:10 #4
Så må du vise lidt mere kode, for det som rtfm foreslog skulle meget gerne virke.

Hvis du henter resultaterne fra en database, kan du jo bede databasen om ikke at returnere poster med tomme titler.
Avatar billede daniel_d Nybegynder
13. januar 2006 - 15:06 #5
Disse data er fra msn via deres API - her er der noget kode:

        <?php
if($query != "")
{
    if($_GET['offst'] == "")
    {
        $offset = 0;
    }
    else
    {
        $offset = $_GET['offst'];
    }
    // Now let's set up the parameters for our API query.
    $parameters = array(
        'AppID' => MSN_API_KEY,
        'Query' => $query,
        'CultureInfo' => 'da-DK',
        'SafeSearch' => 'Off',
        'Requests' => array(
            'SourceRequest' => array(
                'Source' => $source,
                'Offset' => $offset,
                'Count' => $count,
                'ResultFields' => 'All'
            )
        )
    );
    // Set up our call to SOAP to send our request off.
    $soapClient =& new soapclient(MSN_API_ENDPOINT);
   
    // Configure our call to MSN's API
    $result =& $soapClient->call('Search', array('Request'=>$parameters), MSN_API_NAMESPACE);

    // In case we run into an error from SOAP
    if($soapClient->getError())
    {
        $err = $soapClient->getError();
        $result = false;
    }
}

        // If there are no SOAP errors we want it to do it's thing
        if($err == "")
        {
            // Get our results array
            $results = $result['Responses']['SourceResponse']['Results']['Result'];
            // Get an offset for our Next and Previous links
            $nbr = $offset;
           
            if(count($results) > 0)
            {
                // The prev offset for our previous link
                $prevoffset = $nbr - $count;
                // The next offset for our Previous link
                $nextoffset = $nbr + $count;
                // Increase our number, since they'll begin at 0 and we want
                // to use it later to output some numbers to the screen
                // as we list the returned sites.
                $nbr++;
            }
                // If the Previous Offset is NOT a negative number it means we're no
                // longer on the first page.  For the first page it'll always be a
                // negative number, so we won't display a Previous link until
                // prevoffset is at least zero.
                if($prevoffset > -1)
                {

                }
                // If Next Offset is less than the total number of results returned we
                // should show a Next link.
                if($nextoffset < $result['Responses']['SourceResponse']['Total'])
                {
                    // This is a bit of strange logic and is ONLY used to control whether
                    // a div tag is written or not.  It has nothing to do with the API
                    // script really.  Just formatting.  Basically, if a Previous link is
                    // showing, we do NOT want to write another div tag.  However if we're
                    // on the first page we need one to complete the display.
                    // What the line below is really doing is taking care of that.
                    if($prevoffset == $count - ($count * 2))
                    {

                    }
                    ?>
                    <?php   
                }
                // Close out our navigation div and get ready to display the results.
                // Now let's loop through our results array one at a time
                // and write the data to the screen in list tags.
                foreach($results as $res)
                {
                    ?>
                    <div class="katalogdiv"><!-- google_ad_section_start -->
                    <b><?php echo $res['Title']; ?></b><br />
                    <?php echo $res['Description']; ?>&nbsp;<a href="<?php echo $res['Url']; ?>" style="color: #333333; text-decoration: none; font-size: 10px; font-weight: bold;" rel="nofollow"><?php echo $res['Title']; ?></a>
                    </div>
                    <?php
                    // Did you see where we used the nbr variable above to write the
                    // ranking position to the screen?  We need to increase it by one
                    // before the next loop.
                    $nbr++;
                    }


                // Almost done.  This is an exact copy of the Previous - Next link
                // code from above, so I won't comment it again.
                if($prevoffset > -1)
                {
   
                }
                if($nextoffset < $result['Responses']['SourceResponse']['Total'])
                {
                    if($prevoffset == $count - ($count * 2))
                    {
                        ?>
                        <div class="nav">
                        <?php
                    }
                    ?>
                    <?php   
                }
            }
            else
            {
            // If we couldn't do anything because of a SOAP error we'll want to
            // say that instead of displaying nothing.
            echo $err;
            }
            ?>
Avatar billede daniel_d Nybegynder
22. marts 2006 - 15:27 #6
Fik aldrig noget svar på denne, så jeg lukker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester