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条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条