duanci1858 2015-09-21 07:39
浏览 5
已采纳

PHP语言和数据库

is a long question but I hope you can help me

Well I have added a system to switch language on my site but now I want to make the language file dynamyc...

I will try to explain everything so:

I have this file to create a slide on my home page:

<?PHP

$sql = "SELECT img_url, caption  FROM cc_homeslide  ORDER BY position asc";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "<div><figure><div class='dark-effect'><img class='lazyOwl' data-src='images/slide_home/fake_img.png' alt='Awesone Food' style='background-image: url(images/slide_home/". $row["img_url"]. ")' /></div><figcaption>". $row["caption"] ."</figcaption></figure></div>";
    }
} else {
    echo "0 results";
}

?>

Perfect is working. But now I want to switch $row["caption"] to $lang["SLIDE_CAPTION"]

Why? Because in my file en.php I have this:

<?php
/*
------------------
Language: English
------------------
*/

$lang = array();

 //HEAD

$lang['PAGE_TITLE'] = 'Food Delivery from quality restaurants in Hong Kong';
$lang['PAGE_DESCRIPTION'] = 'Order Food from your favorite restaurant and delivers them to your home or office ● Pay Online or Cash On Delivery';
$lang['PAGE_KEYWORDS'] = 'order food online, food delivery in Hong Kong, quality restaurants delivery';

// MENU

$lang['LANG'] = 'Eng';
$lang['LOC_HK'] = 'Hong Kong';
$lang['LOC_KW'] = 'Kowloon';

//SLIDE
require_once ("connect.php");

$sql = "SELECT img_url, caption  FROM cc_homeslide  ORDER BY position asc";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $lang['SLIDE_CAPTION'] = $row["caption"];
    }
}

?>

but the problem is that I can not make the caption match with the right row on the database. With this code I am getting the same caption for all the rows on the database...

So I need to make $lang['SLIDE_CAPTION'] = "caption on each row"

Any idea?

I have tried to explain in the best way but if you are interested and can not understand let me know and I am going to explain again

  • 写回答

1条回答 默认 最新

  • dongmacuo1193 2015-09-21 07:46
    关注

    Each key in an array must be unique. Your loop generating $lang['SLIDE_CAPTION'] = $row["caption"]; is returning each row value into the same variable - so $lang['SLIDE_CAPTION'] will only ever be the last value from your loop.

    Build a $slideCaptions array to hold all captions, using a unique field from the database table as the key:

    $slideCaptions = array();
    
    while($row = mysqli_fetch_assoc($result)) {
        $slideCaptions[$row["id"]] = $row["caption"];
    }
    

    Then, access the caption in your array using the key to output value:

    while($row = mysqli_fetch_assoc($result)) {
        echo "<div>...</div>
              <figcaption>". $slideCaptions[$row["id"]] ."</figcaption></div>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?