添加标题从数组项 [英] Add titles to items from array

查看:116
本文介绍了添加标题从数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有用PHP那么多的经验,我期待与以下帮助:

I don't have that much experience with PHP and am looking for help with the following:

我从具有列的SQL表中读取数据国家包含一个国家的名字和列说明包含评论。

I am fetching data from an SQL table that has a column "countries" containing the name of a country and a column "desc" containing a comment.

在我的网页我创建一个数组出匹配的国家(根据某些标准),以便它们按字母顺序排序,并在他们每个人的前面添加一个图像(图像具有相同的名称作为国家)。
最后,我只是呼应了这阵,以显示国家和在页面上相应的图像。
所有这一切都运作良好,到目前为止(使用低于code)。

On my web page I create an array out of the matching countries (based on certain criteria) in order to sort them alphabetically and to add an image in front of each of them (images have the same name as the countries). Finally I am just echoing out this array in order to display the countries and corresponding images on the page. All of this works well so far (using the below code).

我的code:

$countries = '';
$valC = '';
$countC = 0;
foreach ($objDays->days as $days) {
    if(($days->dateMatch == "Yes") && ($days->locales != "")) {
        $inputC[] = explode(',', $days->locales);
        $countC++;
    }
}
if($countC == 0) {
    $countries = "&nbsp;<img src='images/icons/emotion_what.png' alt='' />&nbsp;&nbsp;No data on file for this date.";
} else {
    $arrC = array_map("trim", call_user_func_array("array_merge", $inputC));
    sort($arrC);
    array_walk($arrC, function (&$valC) { $valC = "<img src='images/icons/flags-32/flag_" . str_replace(" ", "_", $valC) . ".png' alt='' id='c" . $valC . "' style='width:32px' />&nbsp;" . $valC; } );
    $countries = implode(' &nbsp;&nbsp;', $arrC);
}
// ...
echo $countries;

更新:
爆炸不再需要像现在总是有只有一个国家在表的每个单元格。

Update: The explode is no longer needed as now there is always only one country in each cell of the table.

什么我不能做的是以下内容:
我想也从我的专栏添加评论说明为(悬停)标题,我呼应了每一个国家。
我可以通过 $ objDays-&GT提货;天 - &GT;说明 RESP。 $天 - 方式&gt;说明,但无法找到一个方法来添加此

What I am unable to do is the following: I would like to add also the the comments from my column "desc" as a (hover) title to each country that I am echoing out. I can fetch this via $objDays->days->desc resp. $days->desc but can't find a way to add this in.

这里有人能帮助我?

推荐答案

您可以存储$ inputC像这里面的子数组:

You can store a sub array inside $inputC like that :

foreach ($objDays->days as $days) {
    if(($days->dateMatch == "Yes") && ($days->locales != "")) {
        $inputC[] = array(
            "text" => explode(',', $days->locales),
            "desc" => $days->desc
        );
        $countC++;
    }
}

和修改array_walk

And modify your array_walk

array_walk($arrC, function (&$valC) { $valC = "<img src='images/icons/flags-32/flag_" . str_replace(" ", "_", $valC['text']) . ".png' alt='".$valC['desc']."' id='c" . $valC['text'] . "' style='width:32px' />&nbsp;" . $valC['text']; } );

编辑:您的修剪和放大器; array_merge将肯定断裂,需要被更新。

edit : Your trim & array_merge will be certainly broken and need to be updated.

这篇关于添加标题从数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆