doubo4336 2016-06-24 09:39
浏览 56
已采纳

PHP中的函数的新功能,并将它们与foreach循环一起使用

I'm fairly new to using PHP functions, up until now my code has largely consisted of holding a bunch of queries in a file (queries.php) and call them into my main pages using foreach loops.

Now I'm building a small network site that has a lot of dynamic data to deal with.

I've heard that functions are the way to go for this kind of project and although I have tried to convert some of my queries into functions and my for each loops into function calls I haven't been successful - often receiving parse errors.

I do pretty well by being shown an example and adapting it to my needs but have struggled with what tutorials I have been able to find on the net. I have included some sample code in the hope that someone can show me and I can go from there.

I guess I am asking to be guided through how I would convert my current query into a function and calling the function instead of using a foreach loop or having to call the same query multiple times?

An example of a function;

function getProfileGridsWithLocations(){
    // BEGIN QUERY TO DISPLAY MEMBERS WITH LOCATION ON HOMEPAGE
    $query = "SELECT 
            users.id AS id, 
            users.username AS username,
            users.email as email,
            users.created,
            profilephotos.profilephoto_file AS photo,
            profiles.profile_displayname AS displayname,
            profiles.profile_displayage AS displayage,
            locations.user_id,
            locations.location_city,
            frmlocations.location_id,
            frmlocations.city AS City
        FROM users

        JOIN profilephotos ON users.id = profilephotos.user_id
        JOIN profiles ON users.id = profiles.user_id
        JOIN locations ON users.id = locations.user_id
        JOIN frmlocations ON locations.location_city = frmlocations.location_id
        WHERE locations.location_city = '$location' OR '$location' = ''
        ORDER BY users.created DESC
        /*LIMIT 20*/
    "; 
    try 
    { 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    {  
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $members_with_locations_homepage = $stmt->fetchAll();
}

An example of my foreach loop;

<div id="profiles" class="pages">
  <div class="mem-grid-wrap">
  <?php foreach($this->getProfileGridsWithLocations as $members_with_location_homepage): ?> 

    <div class="box">
        <div class="boxInner" style="background-color:#000000;">
          <a href="girl.php?username=<?php echo htmlentities($members_with_location_homepage['username'], ENT_QUOTES, 'UTF-8'); ?>&uid=<?php echo htmlentities($members_with_location_homepage['id'], ENT_QUOTES, 'UTF-8'); ?>">

<?php 
  echo (!empty($members_with_location_homepage['photo'])) ? "<img src='uploads/profile-photos/" . $members_with_location_homepage['photo']. "' />" : "<img src='assets/img/image-not-available.jpg' />"; 
?>
            <div class="titleBox" style="text-align:left; line-height:20px;">
              <span style="font-size:18px; font-weight:bold;"><i class="fa fa-female"></i> <?php echo htmlentities($members_with_location_homepage['displayname'], ENT_QUOTES, 'UTF-8'); ?>, <?php echo htmlentities($members_with_location_homepage['displayage'], ENT_QUOTES, 'UTF-8'); ?></span>
              <br />
              <span style="font-size:11px;"><i class="fa fa-map-marker"></i> <?php echo htmlentities($members_with_location_homepage['City'], ENT_QUOTES, 'UTF-8'); ?> &nbsp;|&nbsp; <i class="fa fa-clock-o"></i> Now</span>
            </div>
          </a>
        </div>
    </div>

  <?php endforeach; ?> 
  </div>
</div>

I've not included the query that I'm currently using as I have used that in the function, provided above.

  • 写回答

3条回答 默认 最新

  • douzhuang6321 2016-06-24 10:04
    关注

    You are not calling your getProfileGridsWithLocations function correctly or returning anything from it.

    Simplified example:

    <?php
    function foo() {
        $result = array('big','fat','mamma');
        return $result;
    }
    
    foreach(foo() as $item) {
        echo $item;
    }
    

    You'll also need access to $db inside getProfileGridsWithLocations. You can pass it into the function as a parameter, or set it as a global.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?