1


Iam a New Beginner of PHP
Here is My Table's Sceenshot.Click Here to see Sceenshot
I want to display All value of "education" .

Such as :

 My 1st value is 53
 My 2nd value is 43
 My 3rd value is 57
 My 4th value is 44
4
  • explode by , and iterate over result array. Commented Oct 4, 2018 at 11:52
  • I want to display value one by one in line...& also assign One value in one variable . Commented Oct 4, 2018 at 11:54
  • And what stops you from using explode? Commented Oct 4, 2018 at 11:55
  • Please have a read of stackoverflow.com/questions/3653462/… Commented Oct 4, 2018 at 11:59

2 Answers 2

1

As u_mulder pointed out, you can use explode to split the string into an array of values and then iterate over that list - http://php.net/manual/en/function.explode.php

Below, I have defined a new function called ordinal which will output 1st, 2nd, 3rd etc. given any number.

In addition to that, you can sprintf to format a string with placeholders.

Example http://sandbox.onlinephpfunctions.com/code/1459ec55d6bc9f28a03645625a22261ede093342

EDIT Added code to turn on error reporting.

<?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];
}
Sign up to request clarification or add additional context in comments.

5 Comments

I don't Know, why this code is not working in my server ?
Me neither. You need to tell us what error msgs you're seeing.
I've added some code at the top to turn on error reporting.
I use live server ....//Page is Loading but its not seen error...when i remove your code....page coming successfully
Sorry, without any error msgs I can't help. My suggestion would be to comment out all the lines and then uncomment it 1 line at a time, to see where the issue is. Good luck!
0
$education = explode(',', $data->education); // explode it using comma 

for($i=0;$i<count($education);$i++){ // iterate the array
    echo $education[$i];
 }

3 Comments

results : 53435744534357445343574453435744534357445343574453435744534357445343574453435744534357445343574453435744534357445343574453435744534357445343574453435744534357445343574453435744
How can i assign one value in one variable ?
why do you want to do that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.