我是一个新的php初学者这是我table上的照片。单击此处查看照片我想展示“教育”的所有价值。例如:
My 1st value is 53 My 2nd value is 43 My 3rd value is 57 My 4th value is 44
My 1st value is 53
My 2nd value is 43
My 3rd value is 57
My 4th value is 44
mu0hgdu01#
正如乌穆德指出的,你可以使用 explode 要将字符串拆分为一个值数组,然后遍历该列表-http://php.net/manual/en/function.explode.php下面,我定义了一个新函数 ordinal 它将输出第1,第2,第3等给定的任何数字。除此之外,你还可以 sprintf 使用占位符格式化字符串。前任amplehttp://sandbox.onlinephpfunctions.com/code/1459ec55d6bc9f28a03645625a22261ede093342编辑添加的代码以打开错误报告。
explode
ordinal
sprintf
<?php// Turn on error reportingini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);// Turn MySQL errors into PHP exceptionsmysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);// Establish DB connection$db = new mysqli("localhost","username","password","dbname");$sql = "select * from tbldatingusermaster order by userid desc";$result = $db->query($sql);while($data = $result->fetch_object()){ // Split the list of values into an array $arrayOfEducationValues = explode(',', $data->education); // Define what you want your output to look like. $format = "My %s value is %d \n"; // Loop over the list of values and then output each one using the // formatted string foreach ($arrayOfEducationValues as $key => $education) { // `$key` here refers to current index of the array. Since // array idexes usually start at 0 you need to add `1` to it. echo sprintf($format, ordinal($key+1), $education); }}/** Taken from https://stackoverflow.com/a/3110033/296555* /function ordinal($number) { $ends = array('th','st','nd','rd','th','th','th','th','th','th'); if ((($number % 100) >= 11) && (($number%100) <= 13)) return $number. 'th'; else return $number. $ends[$number % 10];}
<?php
// Turn on error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Turn MySQL errors into PHP exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Establish DB connection
$db = new mysqli("localhost","username","password","dbname");
$sql = "select * from tbldatingusermaster order by userid desc";
$result = $db->query($sql);
while($data = $result->fetch_object()){
// Split the list of values into an array
$arrayOfEducationValues = explode(',', $data->education);
// Define what you want your output to look like.
$format = "My %s value is %d \n";
// Loop over the list of values and then output each one using the
// formatted string
foreach ($arrayOfEducationValues as $key => $education) {
// `$key` here refers to current index of the array. Since
// array idexes usually start at 0 you need to add `1` to it.
echo sprintf($format, ordinal($key+1), $education);
}
/*
* Taken from https://stackoverflow.com/a/3110033/296555
* /
function ordinal($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13))
return $number. 'th';
else
return $number. $ends[$number % 10];
rbl8hiat2#
$education = explode(',', $data->education); // explode it using comma for($i=0;$i<count($education);$i++){ // iterate the array echo $education[$i]; }
$education = explode(',', $data->education); // explode it using comma
for($i=0;$i<count($education);$i++){ // iterate the array
echo $education[$i];
2条答案
按热度按时间mu0hgdu01#
正如乌穆德指出的,你可以使用
explode
要将字符串拆分为一个值数组,然后遍历该列表-http://php.net/manual/en/function.explode.php下面,我定义了一个新函数
ordinal
它将输出第1,第2,第3等给定的任何数字。除此之外,你还可以
sprintf
使用占位符格式化字符串。前任amplehttp://sandbox.onlinephpfunctions.com/code/1459ec55d6bc9f28a03645625a22261ede093342
编辑添加的代码以打开错误报告。
rbl8hiat2#