回复:什么是最好的方法应该我采用这个 php 问题 [英] re: whats-the-best-approach-should-i-adopt-for-this-php-problem
问题描述
这是关于 问题按照 Shrapnel 上校的建议,发布一个内容清晰的新问题.
我有一个 facebook 用户喜欢数据,即
<前>{数据": [{"name": "小丑","类别": "公众人物"},{"name": "拉斐尔·纳达尔",类别":运动员"},{"name": "拉西","类别": "公司"},{"name": "杰辛达·巴雷特","类别": "公众人物"},{"name": "可口可乐","类别": "公司"},{"name": "甜心托德",类别":电影"},{"name": "笔记本",类别":电影"},{"name": "不可饶恕",类别":电影"}]}我想计算每个类别的编号(例如这里 Movie=3、company=2 等)并将其保存在 mysql 表中,我还想保存其他表中每个类别的名称(即对于类别 Movie 应该有 Movie(id,name) 表并且所有类别都相同).什么我做的是
<预><代码>$电影=0;//还要在这里定义所有变量(即 $company、$public figure 等),以便它会增加.foreach($like['data'] as $jsondata=>$json){if($json['category']==='电影'){$name_movie=$json['name'];$query1="insert into movies(id,name)values('', '" .mysql_real_escape_string($name_movie)."')";$result1=mysql_query($query1) or die("error in query".mysql_error());$电影++;//最后这个 $movie++ 将被插入到其他表中}别的if($json['category']==='company'){ 做和上面一样的步骤}别的...//类似的所有其他类别
但考虑到速度和一致性问题,它似乎效率低下.
我的问题是我是否使用了正确的方法,或者对于这个场景应该有其他的东西.
这是一个更简洁的代码版本.我重新排列 json 数据以创建按类别排序的数组:
$tables = array('电影', '公司');//等等$categories = array();foreach($like['data'] as $jsondata=>$json){$categories[$json['category']][] = $json['category'];{foreach($categories as $key => $category){if (in_array($key, $tables)){$i = 0;foreach($category as $item){$query1="插入 " .$key ."(id,name)values('', '" .mysql_real_escape_string($item['name'])."')";$result1=mysql_query($query1) or die("error in query".mysql_error());$i++;}$category_count[$key] = $i;}}
this is re: question of question
as suggested by Col. Shrapnel to post a new question with clear contents.
I have a facebook user likes data i.e
{ "data": [ { "name": "Joker", "category": "Public figure" }, { "name": "Rafael Nadal", "category": "Athlete" }, { "name": "Lassi", "category": "Company" }, { "name": "Jacinda Barrett", "category": "Public figure" }, { "name": "cocacola", "category": "Company" }, { "name": "SWEENEY TODD", "category": "Movie" }, { "name": "The Notebook", "category": "Movie" }, { "name": "Unforgiven", "category": "Movie" } ]}
i want to count the no of each category (e.g here Movie=3, company=2 etc) and save it in mysql table and i also want to save the name of each category in other table(i.e for category Movie there should be Movie(id,name) table and same for all categories). what i have done is
$movie=0; // also define all variables here(i.e $company, $public figure etc) so that it would be increamented.
foreach($like['data'] as $jsondata=>$json)
{
if($json['category']==='Movie')
{
$name_movie=$json['name'];
$query1="insert into movies(id,name)values('', '" . mysql_real_escape_string($name_movie). "')";
$result1=mysql_query($query1) or die("error in query".mysql_error());
$movie++; // in the last this $movie++ will be inserted in other table
}
else
if($json['category']==='company')
{ do the same steps as above}
else
.
.
.
//similarly for all others categories
But it seems inefficient as per speed and consistency concern.
My question is am i using the right approach or there should be something else for this cenario.
This is a cleaner version of the code. I rearranged the json data to create an array sorted by category:
$tables = array('movie', 'company'); //etc
$categories = array();
foreach($like['data'] as $jsondata=>$json)
{
$categories[$json['category']][] = $json['category'];
{
foreach($categories as $key => $category)
{
if (in_array($key, $tables))
{
$i = 0;
foreach($category as $item)
{
$query1="insert into " . $key . "(id,name)values('', '" . mysql_real_escape_string($item['name']). "')";
$result1=mysql_query($query1) or die("error in query".mysql_error());
$i++;
}
$category_count[$key] = $i;
}
}
这篇关于回复:什么是最好的方法应该我采用这个 php 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!