2016-03-26 16 views
0

Bir veritabanı sırasına birden çok öğe eklemek ve sonuçları ve karşı kısımlarını virgül olarak döndürmenin bir yolu var mı?Birden çok öğeyi bir veritabanı satırına ekleyin ve sonuçları virgülle ayırın

Örneğin

:

$query = $db->query_first(" 
    SELECT * FROM ". TABLE_PREFIX ."post 
    WHERE url = '" . $drc_url . "' 
"); 
:

POST 
POSTID  URL            HITS 
1   http://google.com,http://facebook.com   35,20 

yüzden şimdi böyle bir şey kullanarak bu sonucu göstermek istiyorum facebook 20. sahipken google, 35 hit vardır, post 1 İçinde 2 url'si var demek

Ancak, yalnızca bir URL'yi karşılık gelen isabet değeriyle bir kerede çekmek istiyorum. Mümkün mü? Eğer öyleyse birisi bana doğru yönde işaret edebilir mi?

şu anda benim php gibi görünür:

$query = $db->query_first("SELECT * FROM ". TABLE_PREFIX ."post WHERE url = '" . $drc_url . "'"); 
     if (!$query){ 
      $db->query_write("INSERT INTO ". TABLE_PREFIX ."redirect (url, hits) VALUES ('" . $drc_url . "', 1)"); 
     } else { 
      $db->query_write("UPDATE ". TABLE_PREFIX ."redirect SET hits = hits + 1 WHERE url = '" . $drc_url . "'"); 
     } 

ama tüm bunları istediğiniz aynı $ POSTID içinde onlar eğer kod içindeki açıklamalara bakınız aynı satırda

+0

Evet, ancak veri tablolarınızı düzgün bir şekilde normalleştiriyorsanız çok daha kolay –

+0

Yukarıdaki gibi, normalleştirme konusuna bakın – Strawberry

cevap

0

koymak olsun.

$drc_url = "http://google.com"; 

/* insert/update */ 
$query = $db->query("SELECT * FROM ". TABLE_PREFIX ."post WHERE url LIKE '%" . $drc_url . "%'"); 
if ($query->num_rows > 0) { 
    // the URL can be in multiple rows - go through each row 
    while($row = $query->fetch_assoc()) { 
     //find the URL position/index within the list 
     //if it's possible that the URLs are already separated by anything else than comma (for example "comma and space", "space", etc), you need to add functionality to be able to find them correctly 
     $urls = explode(',', $row[ 'url' ]); 
     $index = array_search($drc_url, $urls); 
     //same as previous comment goes for the hits row 
     $hits = explode(',', $row[ 'hits' ]); 
     //increment the hits number on the correct value 
     $hits[$index]++; 
     //update the hits (put them back as a string from the array with already incremented value) - use unique identificator in the WHERE clause 
     $db->query("UPDATE ". TABLE_PREFIX ."redirect SET hits = '" . implode(',', $hits) . "' WHERE postid = '" . $row[ 'postid' ] . "'"); 
    } 
} else { 
    // the URL was not found - insert 
    $db->query("INSERT INTO ". TABLE_PREFIX ."redirect (url, hits) VALUES ('" . $drc_url . "', 1)"); 
} 


/* select */ 
//same functionality as above, just not updating 
$query = $db->query("SELECT * FROM ". TABLE_PREFIX ."post WHERE url LIKE '%" . $drc_url . "%'"); 
while($row = $query->fetch_assoc()) { 
    $urls = explode(',', $row[ 'url' ]); 
    $index = array_search($drc_url, $urls); 
    $hits = explode(',', $row[ 'hits' ]); 
    echo "URL " . $drc_url . " has " . $hits[$index] . "hits"; 
} 

Ama Mark Baker yorumunda yazdığı gibi, DB yapısını değiştirmek ve sonra yeni bir yapı üzerine inşa etmek çok daha iyi olacağını

.

İlgili konular