array showing data into 1 row. How to make it to 3 cells and then begin new row.

Please look here
http://altadorcup.sketchedneo.com/tester4.php

As you can see all shield images, and shield textarea is being shown horizontally.

How do I make this show 3 shields with its textarea box below (like I have) and then begin a new line of 3 Shields and their textarea boxes?

Example using smileys:

Now: >:( >:( >:( >:( >:( >:( >:( >:( >:( >:(

How I want:

:frowning: >:( >:(
:frowning: >:( >:(
:frowning: >:( >:(

[php]

<?php include ($_SERVER['DOCUMENT_ROOT'].'/dblink.php'); $sort = mysql_query("SELECT * FROM acshields WHERE checked = 1 ORDER BY id"); while($sort2 = mysql_fetch_array($sort)) { $graphic_image .= " "; $graphic_code .= "CSS Code:
#userinfo .contentModuleContent { background-image: url(\"$sort2[url]\"); background-repeat: no-repeat; background-position: top right; } #userinfo .medText img { visibility: hidden; } #userinfo .medText table table img { visibility: visible; }

HTML Code:

URL:
$sort2[url]

"; $graphic_by .= "$sort2[name] || Made by: $sort2[madeby]"; } ?> table.graphics {width:95%;} table.graphics td {padding:10px;padding-bottom:50px;text-align:center;} table.graphics tr {vertical-align:bottom;} table.goalcounters {width:80%;} table.goalcounters td {padding:10px;padding-bottom:30px;vertical-align:bottom;width:33%;}
<?=$graphic_image?><?=$graphic_code?><?=$graphic_by?>

[/php]

Hi Ellie –

You’ll want to use php’s modulus operator for this… something like:

[php]
$total = 9; // total from your database results or whatever

echo ‘

’;

for ($i = 1; $i <= $total; $i++) {
if ($i % 3 == 1) {
echo ‘

’;
} else if ($i % 3 == 2) {
echo ‘’;
} else {
echo ‘’;
}
}
/*
Output:
’ . $i . ‘’ . $i . ‘’ . $i . ‘
1 2 3
4 5 6
7 8 9
*/ [/php]

Of course you’ll want to tweak to your likings but that’s basically it.

Wont that only display 9 results? or will it display 9 results per HTML table row?

9 is ambiguous… I just used it as an example. Update $total to however many you have - $total = count($array) or something. The results will then be divided into 3 columns, row by row.

| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
|10 | 11 |12 |
on and on…

I put the coding like this:

[php]$sort = mysql_query(“SELECT * FROM acshields WHERE checked = 1 ORDER BY id”);

while($sort2 = mysql_fetch_array($sort))

$total = 9; // total from your database results or whatever

echo ‘

’;

for ($i = 1; $i <= $total; $i++) {
if ($i % 3 == 1) {
echo ‘

’;
} else if ($i % 3 == 2) {
echo ‘’;
} else {
echo ‘’;
}
}

{

$graphic_image .= "

";
$graphic_code .= “”;
}

?>

table.graphics {width:95%;} table.graphics td {padding:10px;padding-bottom:50px;text-align:center;} table.graphics tr {vertical-align:bottom;} table.goalcounters {width:80%;} table.goalcounters td {padding:10px;padding-bottom:30px;vertical-align:bottom;width:33%;}
’ . $i . ‘’ . $i . ‘’ . $i . ‘
<img src="$sort2[url]">CSS Code:


#userinfo .contentModuleContent {
background-image: url(”$sort2[url]");
background-repeat: no-repeat;
background-position: top right;
}
#userinfo .medText img {
visibility: hidden;
}
#userinfo .medText table table img {
visibility: visible;
}
  HTML Code: <br>
<textarea><img src=\"$sort2[url]\">
</textarea><p>
    URL: <br>
<textarea>$sort2[url]
</textarea>  </center></td>"; 

$graphic_by .= “

$sort2[name] || Made by: $sort2[madeby]
<?=$graphic_image?><?=$graphic_code?><?=$graphic_by?>

[/php]

and got this

http://altadorcup.sketchedneo.com/tester5.php

(there is not database input there) :frowning:

The old mysql driver your using makes this more difficult than it should be but:

[php]
$sort = mysql_query(“SELECT * FROM acshields WHERE checked = 1 ORDER BY id”);

while($sort2 = mysql_fetch_array($sort)) {
$rows[] = $sort2;
}

$total = count($rows);

echo ‘

’;

for ($i = 1; $i <= $total; $i++) {
if ($i % 3 == 1) {
echo ‘

’;
} else if ($i % 3 == 2) {
echo ‘’;
} else {
echo ‘’;
}
}
[/php]

…and obviously you’ll need to change the markup to suit your design.

’ . $rows[$i][‘url’] . ‘’ . $rows[$i][‘name’] . ‘’ . $rows[$i][‘something’] . ‘

only image url’s appeared on the page… http://altadorcup.sketchedneo.com/tester6.php

[php]<?php
include ($_SERVER[‘DOCUMENT_ROOT’].’/dblink.php’);
$sort = mysql_query(“SELECT * FROM acshields WHERE checked = 1 ORDER BY id”);

while($sort2 = mysql_fetch_array($sort)) {
$rows[] = $sort2;
}

$total = count($rows);

echo ‘

’;

for ($i = 1; $i <= $total; $i++) {
if ($i % 3 == 1) {
echo ‘

’;
} else if ($i % 3 == 2) {
echo ‘
’ . $rows[$i][‘url’] . ‘’ . $rows[$i]['CSS Code:

<textarea rows=“2” cols=“30”>
#userinfo .contentModuleContent {
background-image: url("$sort2[url]");
background-repeat: no-repeat;
background-position: top right;
}
#userinfo .medText img {
visibility: hidden;
}
#userinfo .medText table table img {
visibility: visible;
}
  HTML Code: <br>
<textarea rows=\"2\" cols=\"30\"><img src=\"$sort2[url]\">
</textarea><p>
    URL: <br>
<textarea rows=\"2\" cols=\"30\">$sort2[url]
</textarea>  </center>'] . '</td>';
} else {
  echo '<td>' . $rows[$i]['[name] || Made by: [madeby]']  . '</td></tr>';
}
}

echo ‘

’;

?>
[/php]

oh dear…
First thing, I highly suggest moving all your css to a seperate file(helps keep pages cleaner and easier to edit).
http://www.w3schools.com/css/css_howto.asp

next when you’re using $rows[$i][] the 2nd box here, “[]” is meant to call the column name from your database… depending on how much you’re trying to put into each cell of your table you may want to load everything into variables to help keep things a little clearner like so:

[php]
echo “

”;
for ($i = 1; $i <= $total; $i++) {
$text = “”;
$text2 = "Made by: " . $rows[$i][‘madeby’] . “”;
$nrow = “”;
$crow = “”;
if ($i % 3 == 1) {//this is the first column so we’ll use $nrow to create a new row
echo $nrow;
echo “”;
} else if ($i % 3 == 2) { //this is the 2nd column so it needn’t any special opening/closing
echo “”;
} else { // this is the 3rd column so wei’ll use $crow to close the row
// in this column I’m going to instead of print the url for the picture
// I’m going to show the picture with a textbox that will have the
// person who made it listed inside(provided the column name of the
// db is correct)
echo “”;
echo $crow;
}
}
echo “
”;
echo $rows[$i][‘url’] . “
” . $text;
echo “
”;
echo $rows[$i][‘url’] . “
” . $text;
echo “
”;
echo “<img src=’” . $rows[$i][‘url’] . “’>
” . $text2;
echo “
”;

[/php]

Hope this helps to explain it a little better.

Edit: Sabmin pretty much summed it up during my slow typing :wink:

Looking at the source of the page, you’ve got it. Your table layout is now in dynamically in 3 columns.

Let me explain what the code is doing, then we’ll fix the variable names…

$row[$i][‘field_name’]

$row is all of the data that was returned by your query.
$i is a numerical reference to a specific ‘row’ inside the $row array.
The 3rd part is the name of the column in your database that holds the value you’re looking for. Guessing from what you’ve posted, you have columns called ‘url’, ‘madeby’, etc…

The “for” loop is going to ‘loop’ one time for each row in the $row array. If there were 10 records returned from your query, the loop is going to run 10 times. In each loop, $row[$i] will be refereneced as $row[0], $row[1], $row[2], etc… First row, second row… you get it.

[php]

for ($i = 1; $i <= $total; $i++) {
if ($i % 3 == 1) {
echo ‘

’;
} else if ($i % 3 == 2) {
echo ‘’ . $rows[$i][‘name’] . ‘’;
} else {
echo ’ CSS Code:…background-image: url("$sort2[url]"); ';
}
}[/php]

Again, you’ll need to change the contents of

and as your design needs.

where do i put this on my page or Am I replacing bits??

my code is now

[php]<?php
include ($_SERVER[‘DOCUMENT_ROOT’].’/dblink.php’);
$sort = mysql_query(“SELECT * FROM acshields WHERE checked = 1 ORDER BY id”);

while($sort2 = mysql_fetch_array($sort)) {
$rows[] = $sort2;
}

$total = 9; // total from your database results or whatever
echo ‘

’;

for ($i = 1; $i <= $total; $i++) {
if ($i % 3 == 1) {
echo ‘

’;
} else if ($i % 3 == 2) {
echo ‘’;
} else {
echo ’
’ . $rows[$i][‘name’] . ‘ CSS Code:

<textarea rows=“2” cols=“30”>
#userinfo .contentModuleContent {
background-image: url("$rows[url]");
background-repeat: no-repeat;
background-position: top right;
}
#userinfo .medText img {
visibility: hidden;
}
#userinfo .medText table table img {
visibility: visible;
}
  HTML Code: <br>
<textarea rows=\"2\" cols=\"30\"><img src=\"$sort2[url]\">
</textarea><p>
    URL: <br>
<textarea rows=\"2\" cols=\"30\">$sort2[url]
</textarea>  </center></td></tr>';
}

}
?>
[/php]

and the page displays http://altadorcup.sketchedneo.com/tester6.php

So I played with the total… How do I also set it so it brings all the shields off the database as new ones are constantly added…

You missed the $total I posted:
[php]$total = count($rows);[/php]

Keep in mind, the code I posted just creates columns. You’ll need to update the html inside of the td tags.

Output from my code:

[code]

...
[/code]

Here’s what I would do… create a separate file to act as a variable for your layout of each item.

[php]
$template = ’

CSS Code: <br>
<textarea><style>

#userinfo .contentModuleContent {
background-image: url("$rows[$i][url]");
background-repeat: no-repeat;
background-position: top right;
}
#userinfo .medText img {
visibility: hidden;
}
#userinfo .medText table table img {
visibility: visible;
}

  HTML Code: <br>
<textarea><img src=\"$rows[$i][url]\">
</textarea><p>
    URL: <br>
<textarea>$rows[$i][url]
</textarea>  </center>

$rows[$i][name] || Made by: $rows[$i][madeby]

'; [/php]

No your entire code is going to look like:

[php]<?php

$template = ’

CSS Code: <br/>
<textarea><style>

#userinfo .contentModuleContent {
background-image: url("$rows[$i][url]");
background-repeat: no-repeat;
background-position: top right;
}
#userinfo .medText img {
visibility: hidden;
}
#userinfo .medText table table img {
visibility: visible;
}


HTML Code:


URL:

’ . $rows[$i][url] . ’

' . $rows[$i][name] . ' || Made by: ' . $rows[$i][madeby] . '

';

include ($_SERVER[‘DOCUMENT_ROOT’].’/dblink.php’);
$sort = mysql_query(“SELECT * FROM acshields WHERE checked = 1 ORDER BY id”);

while($sort2 = mysql_fetch_array($sort)) {
$rows[] = $sort2;
}

$total = count($rows);

echo ‘

’;

for ($i = 1; $i <= $total; $i++) {
if ($i % 3 == 1) {
echo ‘

’;
} else if ($i % 3 == 2) {
echo ‘’;
} else {
echo ‘’;
}
}
?>[/php]

It’s not the prettiest, but it should work…

’ . $template . ‘’ . $template . ‘’ . $template . ‘

Thank you!!!

Ok It works…

However, Its not bringing in the info from the database…

http://altadorcup.sketchedneo.com/tester7.php

Image urls are missing :frowning:

Oh… you know what? You need to put the $template = ‘…’; stuff in side the for loop (I wasn’t thinkin’). The $row variables aren’t going to be populated.

Put it between the for loop and the first if statement - then you should be good.

I put it here

[php]
$total = count($rows);

echo ‘

’;
$template = ‘…’;

for ($i = 1; $i <= $total; $i+[/php]

It doesnt show right http://altadorcup.sketchedneo.com/tester7.php

Im so bad at this… Sorry

lol… I didn’t mean literally put “…” i just didn’t want to copy-n-paste all your markup.

Here’s the entire code:
[php]

<?php include ($_SERVER['DOCUMENT_ROOT'].'/dblink.php'); $sort = mysql_query("SELECT * FROM acshields WHERE checked = 1 ORDER BY id"); while($sort2 = mysql_fetch_array($sort)) { $rows[] = $sort2; } $total = count($rows); echo ''; for ($i = 1; $i <= $total; $i++) { $template = '

CSS Code:
#userinfo .contentModuleContent { background-image: url("$rows[$i][url]"); background-repeat: no-repeat; background-position: top right; } #userinfo .medText img { visibility: hidden; } #userinfo .medText table table img { visibility: visible; }
HTML Code:
URL:
' . $rows[$i][url] . '

' . $rows[$i][name] . ' || Made by: ' . $rows[$i][madeby] . '

'; if ($i % 3 == 1) { echo ''; } else if ($i % 3 == 2) { echo ''; } else { echo ''; } } ?>[/php]
' . $template . '' . $template . '' . $template . '

OMG!! Thank you! looks great,

Ok hopefully this is the last issue… whats the last box? http://altadorcup.sketchedneo.com/tester7.php

and to make it pretty I can just do some css like table.example{border etc etc and then do

[php]
$total = count($rows);

echo ‘

’;

for ($i = 1; $i <= $total; $i++) {[/php]

?

Sponsor our Newsletter | Privacy Policy | Terms of Service