简体   繁体   English

PHP:(PDO)Mysql最有效的方法是将数据添加到db中返回值

[英]PHP: (PDO) Mysql most effective way to add sting to returned value out of db

In a mysql database I store some image names without the exact path. 在mysql数据库中,我存储了一些没有确切路径的图像名称。 So what I like to do is add the path before I load the returned array into jQuery (json) to use it in the JQ Galleria plugin. 所以我喜欢做的是在将返回的数组加载到jQuery(json)之前添加路径,以便在JQ Galleria插件中使用它。

In the columns I've got names likes this: 在列中我有这样的名字:

  11101x1xTN.png 11101x2xTN.png 11101x3xTN.png

Which in the end should be like: 最终应该是这样的:

  ./test/img/cars/bmw/11101/11101x1xTN.png
  ./test/img/cars/bmw/11101/11101x2xTN.png

I could just add the whole path into the database but that seems 1. a wast of db space. 我可以将整个路径添加到数据库中,但这似乎是一个db空间的浪费。 2. Then I need to update he whole db if the images path changes. 2.如果图像路径发生变化,我需要更新整个数据库。 I could edit the jQuery plugin but it doesn't seem practical to update the source code of it. 我可以编辑jQuery插件,但更新它的源代码似乎不太实际。

What is the right thing to do and the fasted for processing? 什么是正确的做法和禁食加工? Can you add a string after you make a db query and before you fetch the results? 在进行数据库查询之后和获取结果之前,是否可以添加字符串?

part of the function where I make the query: 我进行查询的函数的一部分:

    $needs = "thumb, image, big, title, description";
    $result = $get_queries->getImagesById($id, $needs);


    $sth=$this->_dbh->prepare("SELECT $needs FROM images WHERE id = :stockId");     
    $sth->bindParam(":stockId", $id);
    $sth->execute();
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);

this is the foreach loop: 这是foreach循环:

$addurl = array('thumb', 'image', 'big');

foreach ($result as $array) {
    foreach ($array as $item => $val) {
        if (in_array($item, $addurl)){

            $val = '/test/img/cars/bmw/11101/'.$val;                
        }

    }
}

the array looks like this: 数组看起来像这样:

Array
(
[0] => Array
    (
        [thumb] => 11101x1xTN.png
        [image] => 11101x1xI.png
        [big] => 11101x1xB.png
        [title] => Title
        [description] => This a blub.
    )
 )

The url should be add to thumb, image and big. 网址应该添加到拇指,图像和大。 I tried to change the array values using a foreach loop but that didn't work. 我尝试使用foreach循环更改数组值,但这不起作用。 Also not noting if the use of that would course a unnecessary slowdown. 也没有注意到使用它是否会导致不必要的放缓。

Assuming your array looks like this: 假设你的数组看起来像这样:

$result = array("11101x1xTN.png", "11101x2xTN.png", "11101x3xTN.png");

A simple array_map() can be used. 可以使用简单的array_map()

$result_parsed = array_map(function($str) { return './test/img/cars/bmw/11101/'.$str; }, $result);

As seen Here 可以看出这里

well, you almost nailed it. 好吧,你几乎钉了它。 only thing you forgot is to store your $val back in array. 你唯一忘记的就是将你的$ val存回数组中。

foreach ($result as $i => $array) {    
    foreach ($array as $item => $val) {    
        if (in_array($item, $addurl)){    
            $val = '/test/img/cars/bmw/11101/'.$val;
            $result[$i][$item] = $val;
        }    
    }    
} 

however, I'd make it little shorter 但是,我会把它缩短一点

foreach ($result as $i => $array) { 
    foreach ($addurl as $item) { 
            $result[$i][$item] = '/test/img/cars/bmw/11101/'.$array[$item];
        } 
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM