我正在使用jQuery DataTables。我有这个代码正常工作:
$res = $this->connection->prepare("....");
$res->bindParam(1,$id);
$res->execute();
$result = $res->fetchAll(PDO::FETCH_ASSOC);
$data = array(
'data'=>$result
);
return json_encode($data);
这就是结果如何。
{“data”:[{“id”:“10”,“time_in”:“2015-07-06 07:30:00”,“time_out”:“2015-07-06 10:26:11 “ ,“出席”:“是”},{“id”:“9”,“time_in”:“2015-07-06 08:00:00”,“time_out”:“2015-07-06 11点46" 分36秒, “参加”: “是”}]}
但是我希望我的id
填充为零,如下所示。
{“data”:[{“id”:“00010”,“time_in”:“2015-07-06 07:30:00”,“time_out”:“2015-07-06 10:26:11 “ ,“出席”:“是”},{“id”:“00009”,“time_in”:“2015-07-06 08:00:00”,“time_out”:“2015-07-06 11点46" 分36秒, “参加”: “是”}]}
我怎样才能做到这一点 这样我可以输出这样的结果,我可以在数据表中显示它吗?
答案 0 :(得分:1)
我更喜欢干净的SQL方法,但你有PHP
标签;所以我无法抗拒。
$res = $this->connection->prepare("....");
$res->bindParam(1, $id);
$res->execute();
$result = $res->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row):
$row['id'] = sprintf('%05d', $row['id']);
endforeach;
$data = array(
'data' => $result
);
return json_encode($data);
答案 1 :(得分:1)
<强>的MySQL 强>
在MySQL中,您可以使用LPAD()。
例如,要使用0到5个字符填充字段id
,请使用以下语句。
SELECT LPAD(id,5,'0') AS id FROM table;
<强>的PostgreSQL 强>
在PostgreSQL中,您可以使用LPAD()。如果id
是整数,则需要将其类型化为字符串。
例如,要使用0到5个字符填充字段id
,请使用以下语句。
SELECT LPAD(id::text,5,'0') AS id FROM table;
答案 2 :(得分:-1)
尝试这个SQL语句,它将解决您的问题:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Untitled Document</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<!-- Menu -->
<div class="menu_main">
<div class="navbar yamm navbar-default">
<div class="container">
<div class="navbar-header">
<div class="navbar-toggle .navbar-collapse .pull-right " data-toggle="collapse" data-target="#navbar-collapse-1" > <span>Menu</span>
<button type="button" > <i class="fa fa-bars"></i></button>
</div>
</div>
<div id="navbar-collapse-1" class="navbar-collapse collapse pull-right">
<ul class="nav navbar-nav">
<li><a href="index.html" class="active"><i class="fa fa-home"></i> Home</a></li>
<li class="dropdown"> <a href="#" data-toggle="dropdown" class="dropdown-toggle"><i class="fa fa-life-ring"></i> Services</a>
<ul class="dropdown-menu multilevel" role="menu">
<li class="dropdown"><a href="#">Web Design Packages<i class="fa fa-angle-right"></i></a>
<ul class="dropdown-menu sub-menu-level1" >
<li><a href="cms_website.html">CMS Website</a></li>
<li><a href="e_commerce.html">E-Commerce Website</a></li>
<li><a href="dynamic_website.html">Dynamic Website</a></li>
<li><a href="website_redesigning.html">Website Redesigning</a></li>
<li><a href="web_hosting.html">Web Hosting</a></li>
<li><a href="bespoke_web.html">Bespoke Web Development </a></li>
</ul>
</li>
<li class="dropdown"><a href="#">E-Marketing<i class="fa fa-angle-right"></i></a>
<ul class="dropdown-menu sub-menu-level1">
<li><a href="seo.html">Search Engine Optimization (SEO)</a></li>
<li><a href="pay_per_click.html">Pay Per Click (PPC)</a></li>
<li><a href="social_media.html">Social Media Marketing</a></li>
<li><a href="e_marketing.html">E-Marketing Strategy</a></li>
<li><a href="competitive_analysis.html">Competitive Analysis</a></li>
<li><a href="content_writing.html">Website Content Writing </a></li>
<li><a href="web_video.html">Web Marketing Video </a></li>
<li><a href="logo_designing.html">Logo Designing </a></li>
<li><a href="borchure_designing.html">Brochure Designing</a></li>
</ul>
</li>
<li class="dropdown"><a href="#">Software Development<i class="fa fa-angle-right"></i></a>
<ul class="dropdown-menu sub-menu-level1">
<li><a href="desktop_application.html">Desktop Applications</a></li>
<li><a href="database_administration.html">Database Administration</a></li>
<li><a href="rfid_tracking.html">RFID Tracking System</a></li>
<li><a href="system_integration.html">System Integration</a></li>
<li><a href="erp_solution.html">ERP Solutions</a></li>
</ul>
</li>
<li class="dropdown"><a href="#">App Development<i class="fa fa-angle-right"></i></a>
<ul class="dropdown-menu sub-menu-level1" >
<li><a href="iphone_app.html">Iphone Apps</a></li>
<li><a href="android_app.html">Android Apps</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown"> <a href="#" data-toggle="dropdown" class="dropdown-toggle"><i class="fa fa-life-ring"></i> IT Support</a>
<ul class="dropdown-menu multilevel" role="menu">
<li><a href="it_crawley.html">IT Support Crawley</a></li>
<li><a href="it_horsham.html">IT Support Horsham</a></li>
<li><a href="it_littlehampton.html">IT Support Littlehampton</a></li>
<li><a href="it_chichester.html">IT Support Chichester</a></li>
<li><a href="it_redhill.html">IT Support Redhill</a></li>
<li><a href="it_worthing.html">IT Support Worthing</a></li>
</ul>
</li>
<li class="dropdown"> <a href="#" data-toggle="dropdown" class="dropdown-toggle"><i class="fa fa-thumbs-up"></i> Consultancy</a>
<ul class="dropdown-menu" role="menu">
<li><a href="software_consultancy.html">Software Consultancy</a> </li>
<li><a href="deployment_services.html">Deployment Service</a> </li>
<li><a href="technical_support.html">Techincal Support</a> </li>
</ul>
</li>
<li class="dropdown"> <a href="#" data-toggle="dropdown" class="dropdown-toggle"><i class="fa fa-life-ring"></i> Price & Packages</a>
<ul class="dropdown-menu multilevel" role="menu">
<li class="dropdown"><a href="#">Web Design Packages<i class="fa fa-angle-right"></i></a>
<ul class="dropdown-menu sub-menu-level1">
<li><a href="basic.html">Basic</a></li>
<li><a href="starter.html">Starter</a></li>
<li><a href="small-business.html">Small Business</a></li>
<li><a href="small-business1.html">Small Business +</a></li>
<li><a href="ecom.html">Ecommerce</a></li>
<li><a href="cms1.html">Content Management </a></li>
</ul>
</li>
<li><a href="seo_packages.html">SEO Packages</a></li>
<li><a href="smo_packages.html">SMO Packages</a></li>
<li><a href="ppc_packages.html">PPC Packages</a></li>
</ul>
</li>
<li> <a href="portfolio.html"><i class="fa fa-file-image-o"></i> Portfolio</a> </li>
<li class="dropdown"> <a href="http://www.digitechitsolutions.co.uk/blog/" class="dropdown-toggle"><i class="fa fa-comment"></i> Blog</a> </li>
<li> <a href="contact_us.html"><i class="fa fa-phone"></i> Contact</a>
</ul>
</div>
</div>
</div>
</div>
<!-- end menu -->
</body>
</html>