dongmangsha7354 2010-10-02 20:19
浏览 31
已采纳

为什么我的小PHP脚本不起作用?

I am writing a script that takes information entered in this format:

John Smith
123 Fake St
Riverwood, 11234
http://google.com

and turns it into

<location id='1'>
   name='John Smith',
   address='123 Fake St',
   city='Riverwood',
   zip=' 11234',
   website='http://google.com'
</location>

Here is the code:

$locations = $_POST['locations'];
$data = explode("
", $locations);
$dataBlocks = explode("

", $locations);
$num = 1;

for ($i=0; $i<count($dataBlocks); $i++) {

    $name = $data[0];
    $address = $data[1];
    $data2 = explode(',', $data[2]);
    $city = $data2[0];
    $zip = $data2[1];
    $website = $data[3];

    $xml = ("<location id='$num'>
                    name='$name',
                    address='$address',
                    city='$city',
                    zip='$zip',
                    website='$website'
             </location>");

    $num++;

}

Now, the above works for one block of info, but when I have 2 or more blocks like so:

John Smith
123 Fake St
Riverwood, 11234
http://google.com

Peter Gunderson
345 Some St
Woodsdale, 44566
http://yahoo.com

Then I get this output:

<location id='1'>
   name='John Smith',
   address='123 Fake St',
   city='Riverwood',
   zip=' 11234',
   website='http://google.com'
</location>

<location id='2'>
   name='John Smith',
   address='123 Fake St',
   city='Riverwood',
   zip=' 11234',
   website='http://google.com'
</location>

I know why this is, it's because I am defining these variables:

$name = $data[0];
$address = $data[1];
$data2 = explode(',', $data[2]);
$city = $data2[0];
$zip = $data2[1];
$website = $data[3];

based on there places in the $data array. So, even though I'm using a loop the values of those variables are always the same. For example: whereas for John Smith the $name variable should have a value of $data[0], for Peter Gunderson the name variable should have a value of $data[5]. But now it's $data[0] for both. Hence the output is showing the same name, address, etc. for every info block entered.

So the question is, how can this script be modified so that the output will show unique results for every info block and not just repeat the first one?

EDIT

@gazler your code gives me this output for the above example:

<location id='1'>
                    name='John Smith',
                    address='123 Fake St',
                    city='Riverwood',
                    zip=' 11234',
                    website='http://google.com'
</location>

<location id='2'>
                    name='',
                    address='Peter Smith',
                    city='',
                    zip='',
                    website='Woodsdale, 44566'
</location>
  • 写回答

3条回答 默认 最新

  • duanpuchun5275 2010-10-02 20:29
    关注

    You need to update $data within your loop over $dataBlocks:

    $locations = $_POST['locations'];
    $xml = '';
    $dataBlocks = explode("
    
    ", $locations);
    $num = 1;
    
    for ($i=0; $i<count($dataBlocks); $i++) {
        // get data for current datablock
        $data = explode("
    ", $dataBlocks[$i]);
        $name = $data[0];
        $address = $data[1];
        $data2 = explode(',', $data[2]);
        $city = $data2[0];
        $zip = $data2[1];
        $website = $data[3];
    
        $xml .= ("<location id='$num'>
                        name='$name',
                        address='$address',
                        city='$city',
                        zip='$zip',
                        website='$website'
                 </location>");
        $num++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64