Laravel: - 在控制器中调用函数来查看

时间:2016-08-29 02:56:23

标签: php laravel

我是Laravel的新手,我不太清楚如何使用它。我有一个ProfileController.php,我有所有代码,我又添加了一段代码来获取记录。现在我希望这条记录显示在特定的Profile.blade.php中,我无法弄明白。需要一些如何执行此操作的指南。请在下面找到我的代码: -

ProfileController.php

    public function showTimings() {

        $con = mysql_connect('localhost','****','****');
                mysql_select_db('lblue_db',$con);

        if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_DetailRS1 = "-1";
if (isset($_GET['recordID'])) {
  $colname_DetailRS1 = $_GET['recordID'];
}
mysql_select_db($database_SEO, $SEO);
$query_DetailRS1 = sprintf("SELECT business.id , business.created_at , business.updated_at , business.deleted_at , business.BUSINESS_NAME ,address.PHONE_FIXED,  address.STREET_NO , address.STREET_NAME, suburb.NAME as 'Suburb', address.POSTCODE  ,  address.PHONE_MOBILE, business.EMAIL , business.ACTIVE , business.WEBURL , business.FACEBOOK_URL , business.GOOGLEPLUS_URL , business.TWITTER_URL , business.YOUTUBE_URL , business.AD_SIZE , business.BOLD_LISTING , business.START_DATE , business.category_id  , category.NAME , business.region , business.FILE_LOCATION , business.CLIENT_ID , business.OFFER_TEXT , business.CONTRACT_ID, SEO_KeywordsDesc.businessID, SEO_KeywordsDesc.keywords, SEO_KeywordsDesc.desc, SEO_KeywordsDesc.monO, SEO_KeywordsDesc.tueO, SEO_KeywordsDesc.wedO, SEO_KeywordsDesc.thurO, SEO_KeywordsDesc.friO, SEO_KeywordsDesc.satO, SEO_KeywordsDesc.sunO, SEO_KeywordsDesc.monC, SEO_KeywordsDesc.tueC, SEO_KeywordsDesc.wedC, SEO_KeywordsDesc.thurC, SEO_KeywordsDesc.friC, SEO_KeywordsDesc.satC, SEO_KeywordsDesc.sunC, SEO_KeywordsDesc.phO, SEO_KeywordsDesc.phC, SEO_KeywordsDesc.pubClosed, SEO_KeywordsDesc.alwaysOpen, seo_keywordsdesc.keywords    ,seo_keywordsdesc.desc FROM business LEFT JOIN category ON business.category_id = category.id Left JOIN address ON business.id= address.business_id Left JOIN SEO_KeywordsDesc ON business.id= SEO_KeywordsDesc.businessID Left JOIN suburb ON  address.SUBURB_ID = suburb.id WHERE business.id = %s", GetSQLValueString($colname_DetailRS1, "int"));
$DetailRS1 = mysql_query($query_DetailRS1, $SEO) or die(mysql_error());
$row_DetailRS1 = mysql_fetch_assoc($DetailRS1);
$totalRows_DetailRS1 = mysql_num_rows($DetailRS1);
$count = mysql_num_rows($DetailRS1);
if($count == 0){
    $output = 'The Store is open 24 hours';
}else{

        $id = $row_DetailRS1['id'];
        $businessName = $row_DetailRS1['BUSINESS_NAME'];
        $fixedPhone = $row_DetailRS1['PHONE_FIXED'];
        $fixedMobile = $row_DetailRS1['PHONE_MOBILE'];
        $active = $row_DetailRS1['ACTIVE']; ;

        $adSize = $row_DetailRS1['AD_SIZE']; 

        $categoryName =  $row_DetailRS1['NAME'];
        $region = $row_DetailRS1['region']; 
        $streetNo = $row_DetailRS1['STREET_NO'];
        $streetName = $row_DetailRS1['STREET_NAME'];
        $suburb = $row_DetailRS1['Suburb'];
        $postcode = $row_DetailRS1['POSTCODE'];
        $clientID = $row_DetailRS1['CLIENT_ID'] ;

        $contractID = $row_DetailRS1['CONTRACT_ID'] ;

            $keywords = $row_DetailRS1['keywords'];
            $description = $row_DetailRS1['desc'] ;
            $mon  = $row_DetailRS1['Monday'] ;
            $tue  =  $row_DetailRS1['Tuesday'] ;
            $wed  = $row_DetailRS1['Wednesday'] ;
            $thur  = $row_DetailRS1['Thursday'] ;
            $fri  = $row_DetailRS1['Friday'] ;
            $sat  = $row_DetailRS1['Saturday'] ;
            $sun  = $row_DetailRS1['Sunday'] ;
            $pubhol  = $row_DetailRS1['Public Holiday'] ;
            $alwaysopen  = $row_DetailRS1['Always Open'] ;





            $lid .= $name . "Business ID";

            $lBUSINESS_NAME .= $name .  "Business Name";
            $lfixedPhone .= $name . "Fixed Number";
            $lmobilePhone .= $name .    "Mobile Number";
            $lACTIVE .= $name . "   Active";
            $lAD_SIZE .= $name .    "   AD Size";

            $lNAME .= $name .   "   Category Name";
            $lregion .= $name . "   Region";

            $lCLIENT_ID .= $name .  "Client ID";

            $lCONTRACT_ID .= $name .    "Contract ID";


            $keywords .= $name .    "Keywords: ";
            $description .= $name . "Description: ";
            $lCONTRACT_ID .= $name .    "Contract ID";
            $mon .= $name . "Monday";
            $tue .= $name . "Tuesday";
            $wed .= $name . "Wednesday";
            $thur .= $name .    "Thursday";
            $fri .= $name . "Friday";
            $sat .= $name . "Saturday";
            $sun .= $name . "Sunday";
            $pubhol .= $name .  "Public Holiday";
            $alwaysopen .= $name .  "Always Open";
        }


           date_default_timezone_set('Australia/Melbourne');
  $datetime = new \DateTime();

  $listItem = array('<li class="active">', '</li>');

 $curDay = date('l');

  $status = array( "Now Open" ,"Closed",  "Opening Soon",  "Closing Soon", "Open 24 hours", "  ");

  $times = $times = array(
    1 => array('day' => 'Monday', 'open' => date( $row_DetailRS1['monO'] ), 'close' => date( $row_DetailRS1['monC'] )),
    2 => array('day' => 'Tuesday', 'open' => date( $row_DetailRS1['tueO'] ), 'close' => date( $row_DetailRS1['tueC'] )),
    3 => array('day' => 'Wednesday', 'open' => date( $row_DetailRS1['wedO'] ), 'close' => date( $row_DetailRS1['wedC']) ),
    4 => array('day' => 'Thursday', 'open' => date( $row_DetailRS1['thurO'] ), 'close' => date( $row_DetailRS1['thurC']) ),
    5 => array('day' => 'Friday', 'open' => date( $row_DetailRS1['friO'] ), 'close' => date( $row_DetailRS1['friC'] )),
    6 => array('day' => 'Saturday', 'open' => date( $row_DetailRS1['satO'] ), 'close' => date( $row_DetailRS1['satC']) ),
    7 => array('day' => 'Sunday', 'open' => date( $row_DetailRS1['sunO'] ), 'close' => date( $row_DetailRS1['sunC'] )) );



  $html .= "  <a href=''>"; echo $curDay; $html .="</a>

               <table class='table table-striped'  border='0' align='center' cellpadding='10' cellspacing='20'>
               <tr>
                 <td>Days</td>
                 <td><span class='white-text' style='margin-right: 3em;'></td> 
                 <td>Business Hours</td> 
                 <td><span class='white-text' style='margin-right: 3em;'></td>
                 <td>Hours</td>
                </tr>";
  $i = 1;



$cd = $datetime->format('N'); 

$timenow =     date("H:i:s", time());


// Create an array of day numbers that start with current day and loops around
$day_order = array_merge(range($cd, 7), range(1, $cd-1));



 foreach ($day_order as $daynum): {
    $oc = $times[$daynum];
    $openingTime = $oc['open'];
    $closingTime = $oc['close'];
    $openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
    $closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
    if ($cd == $daynum) {
        if ($openingTime == ' ' && $closingTime == ' ') {
            $s = $status[4];
        }
        elseif ($timenow < $openingSoon || $timenow > $closingTime ) {
            $s = $status[1];
        }
        elseif  ($timenow > $openingSoon && $timenow < $openingTime ) {
            $s = $status[2];
        }
        elseif  ($timenow > $closingSoon && $timenow < $closingTime ) {
            $s = $status[3];
        } else {
            $s = $status[0];
        }
    } else {
        $s = " ";
    }

    $html .= "<tr>";
    $html .= "<td>". $oc['day']."</td>";
    $html .= "<td>  <span class='white-text' style='margin-right: 3em;'> </td>";
    $html .= "<td>".$openingTime." to ".$closingTime."</td>";
    $html .= "<td>  <span class='white-text' style='margin-right: 3em;'> </td>";
    $html .= "<td>".$s."</td>";
    $html .= "</tr>";

}
      $datetime->add(new \DateInterval('P1D'));








  endforeach;
  $html .= "</table>";
  echo $html;


    }

和我的 Profile.blade.php

<div>
  // I want my function showTimings to be displayed here
</div>

1 个答案:

答案 0 :(得分:1)

您可以在控制器中使用return view('Profile');

加载视图

如果要包含可以访问的数据(如HTML),可以compact()数据或将函数with()链接到视图方法。

选项1:

return view('profile', compact('data');

return view('profile', compact('data', 'data2', 'data3');

选项2:

return view('profile')->with('data', $data);

return view('profile')->with(['data' => $data, 'data2' => $data2]);

请注意,Laravel会自动假设该文件后缀为.blade.php

在这两个示例中,您都可以访问视图中的$data变量。

但是我需要说,你的PHP代码不是你在Laravel中完全没有做到的。但是在这里教你Laravel太麻烦了,所以我会给你留下这个链接,它将教你开始使用Laravel所需的一切。

https://laracasts.com/series/laravel-5-from-scratch