2016-01-10 100 views
0

下面的代碼工作正常,它從文件夾中隨機選擇一個圖像,並將其作爲背景分配給生成的Div。這工作。如果我再次使用此代碼,但使用它來簡單地放置隨機圖像的img-src,這也可以。但是,以與第一種相同的方式使用代碼會破壞網站。我究竟做錯了什麼?兩個幾乎完全相同的代碼片..一個可以工作,另一個不可以

<!-- BEGIN FEATURED 1 --> 

          <?php 
try { 
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
} 
catch (PDOException $e) { 
    echo $e->getMessage(); 
    echo 'Could not establish a connection to the database.'; 
} 

$query = $conn->prepare('SELECT `articleid`,`title` FROM `news_articles` WHERE featured = 1 ORDER BY RAND() LIMIT 1'); 
$array = array(
    'N' 
); 
$query->execute(); 

$results = $query->fetchAll(PDO::FETCH_COLUMN, 0); 
foreach ($results as $row) { 
} 

$image = get_rand_img('images/featured_images/csgo/'); 
$title = $result['title']; 
echo '<a href="index.php?viewarticle=1&articleid=' . $row . '">';?> 
<div id="featured-image" style="height: 267px; width: 292px; background:url(/images/featured_images/csgo/<?php echo $image ?>)"> 
<?php 

$result = $conn->prepare("SELECT `articleid`,`title`,`short_title` FROM `news_articles` WHERE articleid=$row"); 
    $result->execute(); 
    $rows = $result->fetch(); 
     echo '<div class="featuredtitle1">'; 
     echo $rows['short_title']; 
     echo '</div>'; 
     echo '</div>'; 
     echo '</a>'; 

?> 

下面的代碼,會導致網站..(我曾評論它的那一刻,你可以看到)。

<!-- BEGIN FEATURED 2 (Disabled) --> 

      <?php 
/* 
try { 
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
} 
catch (PDOException $e) { 
    echo $e->getMessage(); 
    echo 'Could not establish a connection to the database.'; 
} 

$query = $conn->prepare('SELECT `articleid` FROM `news_articles` WHERE featured = 1 ORDER BY RAND() LIMIT 1'); 
$array = array(
    'N' 
); 
$query->execute(); 

$results = $query->fetchAll(PDO::FETCH_COLUMN, 0); 
foreach ($results as $row) { 
} 

$image = get_rand_img('images/featured_images/dota/'); 
$title = $result['title']; 
echo '<a href="index.php?viewarticle=1&articleid=' . $row . '">';?> 
<div id="featured-image2" style="height: 267px; width: 292px; background:url(/images/featured_images/dota/<?php echo $image ?>)"> 
<?php 

$result = $conn->prepare("SELECT `articleid`,`title`,`short_title` FROM `news_articles` WHERE articleid=$row"); 
    $result->execute(); 
    $rows = $result->fetch(); 
     echo '<div class="featuredtitle2">'; 
     echo $rows['short_title']; 
     echo '</div>'; 
     echo '</div>'; 
     echo '</a>'; 
*/ 
?> 

我不明白爲什麼第一段代碼很好,但是再次複製它會破壞它?

+0

你得到什麼錯誤? –

+0

嚴格的標準:只有變量應該通過引用傳遞給C:\ xampp \ htdocs \ index.php,行953 – MikeXero

+0

,它是'$ ext = end(explode('。',$ file)); if($ ext =='gif'|| $ ext =='jpeg'|| $ ext =='jpg'|| $ ext =='png'|| $ ext =='GIF'|| $ ext = ='JPEG'|| $ ext =='JPG'|| $ ext =='PNG'){' – MikeXero

回答

0

在編輯您的評論之前,我看到了一個關於以數組的形式訪問對象的致命錯誤。嘗試從這個更改代碼:

$results = $query->fetchAll(PDO::FETCH_COLUMN, 0); 

要這樣:

$results = $query->fetchAll(PDO::FETCH_ASSOC); 
+0

好吧,我簡單地刪除了$ title = $ result ['title'];和上面的代碼現在工作..我會盡你所能地做,進一步優化。 – MikeXero

+0

是的,問題是你作爲一個數組訪問$ result,但它是一個對象。我建議的代碼實際上會給你一個數組。 –

相關問題