Jump to content

empty cell in data in columns (display record from database)


sigmahokies
Go to solution Solved by Psycho,

Recommended Posts

Hi everyone,

 

I am still learning. I am trying to create the display the record in vertical columns, but set up the vertical columns with order by last name in MySQL is success, but problem that all table cell are empty that I write to display the record, seem MySQL's query did not answer or delivery the record from database. What did I do wrong?

 

Here my code:

 

$display = "SELECT ID, FirstName, LastName, MonthEx FROM Members ORDER BY LastName";$result = mysqli_query($Garydb, $display); $data = mysqli_num_rows($result);$row = 1;$col = 2; echo "<table border='1'>";for ($i = 0; $i < $data / $col; $i++) {echo "<tr>";for ($j = 0; $j < $col && $row <= $data; $j++, $row++) {echo "<td>".$data['FirstName']." ".$data['LastName']."</td><td><a href='update.php?update=$data[iD]'>EDIT</a></td><td><a href='delete.php?delete=$data[iD]'>DELETE</a></td>\n";}echo "</tr>";}echo "</table>";

 
funcation is working, but display the record hasn't come out from MySQL...
 
Thank you in advance time.
 
Gary
Link to comment
Share on other sites

So, Do i have to change $result from $data, like $result['FirstName']? I did that...it is not showing, still empty in cell in table. Do I have to create 'while' to code with mysqli_fetch_assoc or mysqli_fetch_array instead of 'for'? Please advise me

 

Thanks

Edited by sigmahokies
Link to comment
Share on other sites

With all due respect, your grammar and punctuation is atrocious. I am not a grammar nazi, but what you wrote is very difficult to understand. If you want to get free help, the least you can do is write a clear request. I can't tell if you want the data to go top-down/left-right or left-right/top-down.

 

Do you want this

 

1  2
3  4
5  6

 

Or this

 

1  4
2  5
3  6
Link to comment
Share on other sites

All right, I admit...English is my ESL - English Second Language. I'm Deaf. I am using American Sign language (ASL), I have problem with English that which conflict with ASL sometimes. I am trying to create a vertical data cell in the table that display the record from database. 

 

Please forgive me, I am very fluent in ASL, then English is my second language...once any second langauge, surely little lower skill in language.

 

I might be not good at english, but I can do PHP.

 

I hope everyone will understand.

 

What i want is...

 

1 4 7

2 5 8

3 6 9

 

Got it? Thank you for be patient with me.

 

Gary 

Edited by sigmahokies
Link to comment
Share on other sites

  • Solution

Try this:

 

 

<?php
 
//Define the number of columns allowed
$max_columns = 3;
 
//Query the data
$query = "SELECT ID, FirstName, LastName, MonthEx FROM Members ORDER BY LastName";
$result = mysqli_query($Garydb, $query);
 
//Put results into an array
$rows = array();
while($rows[] = mysqli_fetch_array($result, MYSQLI_ASSOC)) {}
 
//Determine the number of rows
$max_rows = ceil(count($rows) / $max_columns);
 
//Separate data array into chunks for each column
$data_chunks = array_chunk($rows, $max_rows);
 
//Generate HTML output (into a variable)
$output = '';
//Iterate over the array chunks while not empty
while(count($data_chunks[0]))
{
    //Open new row
    $output .= "<tr>\n";
    //Get next record from each chunk
    foreach($data_chunks as &$chunk)
    {
        //Remove the first element off of the current chunk
        $data = array_shift($chunk);
        if(empty($data)) { continue; }
        //Create the 
        $output .= "<td>{$data['FirstName']} {$data['LastName']}</td>";
        $output .= "<td><a href='update.php?update={$data['ID']}'>EDIT</a></td>";
        $output .= "<td><a href='delete.php?delete={$data['ID']}'>DELETE</a></td>\n";;
    }
    //Close the row
    $output .= "</tr>\n";
}
 
?>
<html>
<head></head>
 
<body>
 
<table border='1'>
  <?php echo $output; ?>
</table>
 
</body>
</html>
Link to comment
Share on other sites

Psycho,

 

It is working now, but I need to learn to do the logic in script in PHP. There are some things in PHP I don't understand fully. Like, I wonder why some people use "%", I know it is about like round, but how it does work? Also, I notice you put - ".=" it is a dot with equal, how it does work? I mean, why do you put ".=" instead of "==" or ">="...? I have few books about PHP, they don't explain very clearly about those. Can you give me a link for advance tutorial in PHP to use those letters?

 

Thanks,

Gary

Link to comment
Share on other sites

Have you ever considered looking at the PHP reference manual?

 

Like, I wonder why some people use "%", I know it is about like round, but how it does work?

http://uk1.php.net/manual/en/language.operators.arithmetic.php

 

Also, I notice you put - ".=" it is a dot with equal, how it does work? I mean, why do you put ".=" instead of "==" or ">="...?

http://uk1.php.net/manual/en/language.operators.assignment.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.