在函数中使用循环 [英] Using a Loop Inside a Function
问题描述
我仍然在学习功能以及它们如何工作。我知道我在做什么错,而不是如何解决它。我正在写一个函数,将图像数据从数据库中提取出来并返回到屏幕上。它可以工作,但是如果有多个图像,它将只返回最后一个图像。我知道问题是因为while循环的作用, $ project_image
变量添加了多个图片。
$ b
缩减功能
函数get_project_image($ id,$ type =thumb,$ src =false,$ limit = 1){
if($ type ==main){
$ project_image_qry = mysql_query(SELECT i_name FROM`project_images` WHERE i_project_id ='$ id'AND i_type ='2'LIMIT $ limit)or die (mysql_error());
$ project_image =;
while($ project_image_row = mysql_fetch_array($ project_image_qry)){
$ project_image_result = mysql_fetch_array($ project_image_qry);
$ b $ if($ src ==true){
$ project_image。='< img src =''。admin_settings('site_url')。admin_settings('image_main_dir')。 '/'.$project_image_result['i_name']。'alt =project_image/>';
}
else {
$ project_image。= $ project_image_result ['i_name'];
}
}
}
return $ project_image;
您的 另外,它似乎默认情况下告诉它只从数据库请求一个图像。您有一个默认参数 I'm still learning functions and how they work. I know what I'm doing wrong just not how to fix it. I am writing a function to pull image data out of a database and return it onto the screen. It works but if there is more than one image it will return the last image only. I know the problem is that Abridged Function
Your Also, it seems like by default you told it to only request one image from the database. You have a default parameter 这篇关于在函数中使用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!而
条件很好,但是您似乎在两次获取中,在 $ project_image_row
和 $ project_image_result
,所以你会跳过其他图像。只需一次获取数组(在while循环中是好的),并使用 $ project_image_result
而不是 $ project_image_row
循环引用该数组:
pre $ while($ project_image_result = mysql_fetch_array($ project_image_qry)){
if($ src ==true){
$ project_image。='< img src =''。admin_settings('site_url')。admin_settings('image_main_dir')。'/'。$ project_image_result ['i_name'] ''alt =project_image/>';
}
else {
$ project_image。= $ project_image_result ['i_name'];
$ / code $ / pre
$ limit = 1
,并且您使用SQL LIMIT
来过滤结果,所以除非您将第四个参数传递给 get_project_image
mysql_query
只返回一个结果(您没有包含调用 get_project_image
,所以我不确定你是否处理了这个问题。$project_image
is only returning the last image because of the way the while loop works but my question is how can i either not use a while loop or make it add more than one image to the $project_image
variable. function get_project_image($id,$type="thumb",$src="false",$limit=1){
if($type =="main"){
$project_image_qry = mysql_query(" SELECT i_name FROM `project_images` WHERE i_project_id = '$id' AND i_type= '2' LIMIT $limit " ) or die(mysql_error());
$project_image="";
while($project_image_row = mysql_fetch_array($project_image_qry)) {
$project_image_result = mysql_fetch_array ($project_image_qry);
if($src=="true"){
$project_image .= '<img src="'.admin_settings('site_url').admin_settings('image_main_dir').'/'.$project_image_result['i_name'].'" alt="project_image"/>';
}
else{
$project_image .= $project_image_result['i_name'];
}
}
}
return $project_image;
}
while
condition is fine, but you seem to be fetching twice, in $project_image_row
and $project_image_result
, so you're going to be skipping every other image. Just fetch the array once (in the while loop is good), and use $project_image_result
instead of $project_image_row
within the loop to refer to that array:while($project_image_result = mysql_fetch_array($project_image_qry)) {
if($src=="true"){
$project_image .= '<img src="'.admin_settings('site_url').admin_settings('image_main_dir').'/'.$project_image_result['i_name'].'" alt="project_image"/>';
}
else{
$project_image .= $project_image_result['i_name'];
}
}
$limit=1
, and you use an SQL LIMIT
to filter the results, so unless you pass a fourth argument to get_project_image
the mysql_query
is only going to return one result (you didn't include the call to get_project_image
, so I'm not sure if you handled that or not