Jump to content
  • 0

JavaScript IE6 многоуровневые select


banyman
 Share

Question

Здравствуйте, тут такая проблема, нужно для JSON вывести массив, но в последней строке не должно быть запятой... подскажите, как определить последнюю сроку для этого.

<?php

$sCountry = '';
$sCity = '';

mysql_select_db($database_Liga, $Liga);
$query_rsCountry = "SELECT * FROM Country";
$rsCountry = mysql_query($query_rsCountry, $Liga) or die(mysql_error());
$totalRows_rsCountry = mysql_num_rows($rsCountry);

while ($row_rsCountry = mysql_fetch_assoc($rsCountry)) {
$sCountry .= '<option value='.$row_rsCountry['Id'].'>'.$row_rsCountry['Country'].'</option>';

mysql_select_db($database_Liga, $Liga);
$query_rsCity = "SELECT * FROM City WHERE City.Country = '".$row_rsCountry['Id']."' ORDER BY City ASC";
$rsCity = mysql_query($query_rsCity, $Liga) or die(mysql_error());
$totalRows_rsCity = mysql_num_rows($rsCity);
$sCity .=" '".$row_rsCountry['Id']."':{ "."\n";
while ($row_rsCity = mysql_fetch_assoc($rsCity)) {
$sCity .=" '".$row_rsCity['Id']."':'".$row_rsCity['City']."',"."\n";
}
$sCity .=" }, ";
}


?>

выводит все с запятыми

'6':{ 
'6':'Москва',
},
'2':{
'1':'Владивосток',
'22':'Владивосток',
'15':'Екатеринбург',
'20':'Иркутск',
'23':'Казань',
'9':'Краснодар',
'19':'Красноярск',
'11':'Липецк',
'7':'Нижний Новгород',
'18':'Новосибирск',
'17':'Омск',
'14':'Оренбург',
'8':'Ростов-на-Дону',
'12':'Самара',
'10':'Санкт-Петербург',
'13':'Уфа',
'21':'Хабаровск',
'16':'Челябинск',
},
'3':{
'4':'Киев',
},
'4':{
'25':'Алматы',
},
};

несколько вариантов перебрал... например

$sCountry = '';
$sCity = '';

mysql_select_db($database_Liga, $Liga);
$query_rsCountry = "SELECT * FROM Country";
$rsCountry = mysql_query($query_rsCountry, $Liga) or die(mysql_error());
$totalRows_rsCountry = mysql_num_rows($rsCountry);

while ($row_rsCountry = mysql_fetch_assoc($rsCountry)) {
$sCountry .= '<option value='.$row_rsCountry['Id'].'>'.$row_rsCountry['Country'].'</option>';

mysql_select_db($database_Liga, $Liga);
$query_rsCity = "SELECT * FROM City WHERE City.Country = '".$row_rsCountry['Id']."' ORDER BY City ASC";
$rsCity = mysql_query($query_rsCity, $Liga) or die(mysql_error());
$totalRows_rsCity = mysql_num_rows($rsCity);
$sCity .=" '".$row_rsCountry['Id']."':{ "."\n";
while ($row_rsCity = mysql_fetch_assoc($rsCity)) {

$count = count( $rsCity );
for($i = 0; $i < $count; $i++){
if( $i == $count - 1){
// последняя строка
$sCity .=" '".$row_rsCity['Id']."':'".$row_rsCity['City']."'"."\n";
} else {
$sCity .=" '".$row_rsCity['Id']."':'".$row_rsCity['City']."',"."\n";
}
}



}
$sCity .=" }, ";
}

выводит все без запятых.......

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy