dongqiyou0303 2014-04-23 21:20
浏览 9
已采纳

无法创建数组

I have a problem… I am trying to create an array from a mysql table. But I don’t know how to format the data coming out of MySQL into an array in php. Here is what I have done so far...

//Generate Org Data
$result_org = mysql_query("SELECT emp_no,sup_empno,Name,Title FROM employees");
// Initializes a container array 
$orgArray = array();
while($row = mysql_fetch_array($result_org, MYSQL_ASSOC))
    {
    $currempno = $row['emp_no'];
    $currsupervisor = $row['sup_empno'];
    $currtitle = $row['Name']. '
 ' .$row['Title'];

// This is where I haven't a clue to get it the right format...??

// Stores each database record to an array

      $buildorg = array("$currempno","$currsupervisor","$currtitle");

    // Adds each array into the container array
        array_push($orgArray, $buildorg);

        }
    // show the data to verify

        echo ($orgArray);


// the data needs to be exactly like this below

o.addNode(003, 002, '', 'Jane Doe Asst Manager');

where 003 is the $currempno 002 is the $currsupervisor Jane Doe Asst Manager is $currtitle

getting the o.addNode( along with the commas and double quotes and ending ); around this has me perplexed

Any help would be appreciated…

K Driscoll

  • 写回答

3条回答 默认 最新

  • dongrui6787 2014-04-23 21:28
    关注

    If I understand correctly, you are actually trying to create line

    o.addNode(003, 002, '', 'Jane Doe
    Asst Manager');
    

    so just put the values into the string and format it the way you want to - there is no need to create another array - you can create final strings and push those into the array:

    $currempno = $row['emp_no'];
    $currsupervisor = $row['sup_empno'];
    $currtitle = $row['Name']. '
     ' .$row['Title'];
    
    $output = sprintf("o.addNode(%03d, %03d, '', '%s');", $currempno, $currsupervisor, $currtitle);
    array_push($orgArray, $output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图