duanfen2349 2014-05-13 17:57
浏览 45
已采纳

为返回的数据添加顺序号

Newb here attempting something new (for me)

What I am attempting: I am attempting to create an idea number that iterates with each return, so the incident report for the giant kaiju (strange beast - aka Godzilla and friends) has an individual case number that is created programmatically). This means return one would be #1, return two would be #2, returned 3 would be #3 et al.

What I have done: I've attempted to put my int at the beginning, in the middle, at the end, after the return, etc, but regardless of where I put it, i always get the same number (12). I've read about iterating over at php.net, and looked through stack overflow and other places but i've not found something i could internalize/understand enough to replicate.

function __toString(){
$kID = 1;//kaiju incident number

    $myReturn = "<p> Incident ID: kID" . $kID ;
    $kID++; //not iterating thru
    $myReturn .= $kID ;

    $myReturn .= " | Massive Terrestial Organsim Reported: " . $this->movTitle . "   " ;
    $myReturn .= " | Location Name: " . $this->entWhat. " ";
    $myReturn .= " | Severity of Incident Reported: " . $this->movRating . "</p>" ;

    return $myReturn;
  • 写回答

2条回答 默认 最新

  • dqd22496 2014-05-13 18:05
    关注

    $kID is set to 1 each time the function is called. Make it static so it will retain it's values across function calls:

    static $kID = 1;//kaiju incident number
    

    Also:

    $myReturn = "<p> Incident ID: kID" . $kID;
    $kID++;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序