WN

WN (https://www.wn.se/forum/index.php)
-   Serversidans teknologier (https://www.wn.se/forum/forumdisplay.php?f=4)
-   -   PHP hjälp (https://www.wn.se/forum/showthread.php?t=1050215)

Hellsing 2011-09-27 13:17

PHP hjälp
 
Hallo!

Jag försöker skriva ut data från två tabeller i min MySQL i en array men gör något fel. Det finns säkert någon bättre metod att göra detta?

Kod:

// Select the ad from first table
$getads = mysql_query("SELECT * FROM wi1_posts WHERE post_status='publish' AND post_type='ad_listing' ORDER BY post_date DESC LIMIT 5");
while($row=mysql_fetch_array($getads)){

// Get image
$getimg = mysql_query("SELECT * FROM wi1_postmeta WHERE post_id=".$row['ID']." AND meta_key='_wp_attachment_metadata'");
while($row2=mysql_fetch_array($getimg)){
           
    echo '<h3>';
    echo $row['post_title'];
    echo '</h3>';
    echo $row2['meta_value'];
    echo "<br />";
    echo $row['post_date'];
    echo "<br />";
    echo $row['post_content'];
    echo "<br /><br /><br />";
}
}


Hellsing 2011-09-27 14:19

Fick det att nästan fungera med detta:
Kod:

$getads = mysql_query("SELECT wi1_posts.post_title, wi1_posts.post_content, wi1_posts.post_date, wi1_postmeta.meta_value, wi1_postmeta.meta_key
    FROM wi1_posts
    INNER JOIN wi1_postmeta
    ON wi1_posts.ID=wi1_postmeta.post_id
    WHERE wi1_posts.post_status='publish' AND wi1_posts.post_type='ad_listing'
    ORDER BY wi1_posts.post_date DESC LIMIT 5");
   
    while($row=mysql_fetch_array($getads)){
       
        echo '<h3>';
        echo $row['post_title'];
        echo '</h3>';
        echo $row['meta_value'];
        echo "<br />";
        echo $row['post_date'];
        echo "<br />";
        echo $row['post_content'];
        echo "<br /><br /><br />";   
       
    }

Problemet är att tabellen wi1_postmeta innehåller följande fält:
meta_id
post_id
meta_key
meta_value

Och jag vill bara skriva ut meta_value där meta_key är "_wp_attached_file". Hur löser man detta månne?

taz76 2011-09-29 20:24

if ( $row['meta_key'] == "_wp_attached_file") {

echo $row['meta_value'];

}

captaindoe 2011-09-30 11:55

PHP-kod:

$getads mysql_query("SELECT wi1_posts.post_title, wi1_posts.post_content, wi1_posts.post_date, wi1_postmeta.meta_value, wi1_postmeta.meta_key
    FROM wi1_posts
    INNER JOIN wi1_postmeta
    ON wi1_posts.ID=wi1_postmeta.post_id
    WHERE wi1_posts.post_status='publish' AND wi1_posts.post_type='ad_listing' AND wi1_postmeta.meta_key='_wp_attached_file'
    ORDER BY wi1_posts.post_date DESC LIMIT 5"
) or die(mysql_error()); 

Inte provat men tror att det fungerar.


Alla tider är GMT +2. Klockan är nu 19:30.

Programvara från: vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Svensk översättning av: Anders Pettersson