2014-01-30 30 views
0

我有很多网站使用一些相同的内容片段,而不是手动更新所有不同的网站,我认为这是一个好主意,将内容存储在数据库中,只有一个副本而不是多个副本。除了文章中的图像有时会左对齐并且其他时间对齐之外,它的效果很好。如何在SQL关联数组中注入一个变量?

我的解决方案是将以下代码添加到数据库中的文章图像CSS标签,并在每个单独页面上使用变量将自定义类添加到图像。

class="<?php echo $ImgClass01; ?>" 

编辑:这里是更多的什么是存储在数据库字段的内容,使我的问题更容易理解。

<p><img src="img/charleston.jpg" class="<?php echo $ImgClass01; ?>">Is it the delightful year-round climate? The almost-European feel of its downtown city streets? The overwhelming...</p> 

但是,网页仅在查看源代码时显示文本,而未使用变量。几乎任何事情都是可能的,但我不知道如何完成这项工作。

这里是网页上的代码...

// value for the class within the article to be printed on the page 
    $ImgClass01 = 'img-responsive img-rounded pull-right'; 

//Start a while loop to process all the rows 
while ($row = mysql_fetch_assoc ($result_set)) 
{ 
    $article = $row['article']; 
    echo $article; 
} //END WHILE 

编辑:就在情况下,整个页面会有所帮助,这里是它。

<?php 
    $PageTitle = "Charleston, South Carolina | Local Towns"; 
    $PageDescription = "Charleston is rated the first most popular vacation destination in the United States, and it surely must rank in..."; 

    // 1. Create a database connection 
    $link = mysql_connect ("localhost", "root", ""); 
    if (!$link) die("Could not connect: " . mysql_error()); 

    // 2. Select a database to use 
    if (!mysql_select_db ("articleBank")) 
     die("Problem with the database: " . mysql_error()); 

    // 3. Set up query for items to display 
    $query = "SELECT article FROM `articles` WHERE ID = 1"; 

    // 4. Execute the query 
    $result_set = mysql_query ($query); 

    include ("theme/header.php"); 

    // value for the class within the article to be printed on the page 
    $ImgClass01 = 'img-responsive img-rounded pull-right'; 

//Start a while loop to process all the rows 
while ($row = mysql_fetch_assoc ($result_set)) 
{ 
    $article = $row['article']; 
    echo $article; 
} //END WHILE 

    // 5. Close Connection 
    mysql_close(); 

    include ("theme/footer.php"); 
    ?> 

回答

0
while ($row = mysql_fetch_assoc ($other_result_set)) 
{ 
$ImgClass01 = $row['ImgClass01']; 
} 

不知道你想要什么,而是你可以使用左,右像的地方有一排..

+0

漏掉了从网页上的报道。也许这可能会让它更清楚地提供存储在数据库中的更多文章。我将编辑我的原始文章以包含此内容。

Brian

相关问题