Jump to content

Recommended Posts

Hi. This is my first question here.

 

echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
?>
<tr>
<?php
     // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>

 

This will print the array like:

([ ] <- cell)

 

[value1]

[value2]

[value3]

[value4]

[value5]

..etc

 

I would like it to it to print and sort all value in 4 cell(collumn) then add a new row and do so on..

Like this

[val1][val2][val3][val4] (add 4cells with the first 4 arrayvalues then a new row)

[val5][val6][val7][val8] (add 4cells with the 4 next arrayvalues then a new)

[val9] etc..

 

Can anyone assist?

Link to comment
https://forums.phpfreaks.com/topic/60708-solved-foreach-array/
Share on other sites

try

<?php
echo "</tr>\n";
// printing table rows
foreach ($rows as $row)
while($row = mysql_fetch_row($result)){
     // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    $count = 0;
foreach($row as $cell){
	if ($count % 4 == 0) echo '<tr>';
	echo "<td>$cell</td>";
	$count++;
	if ($count % 4 == 0) echo '</tr>';
}
while ($count % 4 > 0){
	$count++;
	echo '<td> </td>';
}
echo "</tr>\n";
}
mysql_free_result($result);
?>

Link to comment
https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302024
Share on other sites

Ahh..  ;)

It works.

 

But....... yes a but..

 

The thing is that i want input the array info on all cells..

 

if the array looks like example.

$row= array(1, 2, 3, 4, 5, 6, 7, 8, etc..);

 

i want the first four cells to have get the first 4 values from the array,

then on the next row i want the next 4 cells to have the next 4 values in the array and so on.

 

I cant manage to get this to work. To be honest i have little clue of how i shall make it work.

Link to comment
https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302056
Share on other sites

Here is the whole thing..

 

<html><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'test';
$table = 'test';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table} limit 0, 2");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result)){
     // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    $count = 0;
foreach($row as $cell){
	if ($count % 4 == 0) echo '<tr>';
	echo "<td>$cell</td>";
	$count++;
	if ($count % 4 == 0) echo '</tr>';
}
while ($count % 4 > 0){
	$count++;
	echo '<td>$cell</td>';
}
echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>

Link to comment
https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302081
Share on other sites

Try this...

 

<html><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'test';
$table = 'test';

if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database");
if (!mysql_select_db($database)) die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table} limit 0, 2");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";

// printing table rows
while($row = mysql_fetch_array($result)){
     // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    $count = 0;
foreach($row as $cell){
	if ($count % 4 == 0) echo '<tr>';
	echo "<td>$cell</td>";
	$count++;
	if ($count % 4 == 0) echo '</tr>';
}
while ($count % 4 > 0){
	$count++;
	echo "<td>$cell</td>";
}
echo "</tr>\n";
}
?>
</body></html>

Link to comment
https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302086
Share on other sites

How many records there is in my table??

 

The database is called test and i got one table called test there. Inside the table i have fields (columns), the one i have now is called test.

I have only one so far. But i will add more later on. I have 6 records on test, and more will be added later aswell.

Link to comment
https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302117
Share on other sites

Ok, I see.... try this one

 

<html><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'test';
$table = 'test';

if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database");
if (!mysql_select_db($database)) die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table} limit 0, 2");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";

echo "<table border='1'>";
echo "<tr>";
echo "<td>Test Column 1</td>";
echo "<td>Test Column 2</td>";
echo "<td>Test Column 3</td>";
echo "<td>Test Column 4</td>";
echo "</tr>";
while($row = mysql_fetch_array($result)) {
if ($count % 4 == 0) echo '<tr>';
echo "<td>".$row['test']."</td>";
$count++;
if ($count % 4 == 0) echo '</tr>';
}
echo "</table>";
?>
</body></html>

Link to comment
https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302127
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.