You’ll need to be a bit more precise what you’re after. You’ve got a three column table … what exactly do you want to change?
(Also, it’s much better to post the HTML code (ie, what appears in the source code) than the raw PHP, unless the problem is with the PHP itself, in which case you’re likely to get more help from the PHP forum)
sorry hope this is the right place
as you see in the picture they display the 3 cols downwards i would like to display another set of 3 side by side if you see what i mean so 6 cols altogether
cheers
Doug
ps can you move this to php seems it should be there
Do you want the table to show twice as many columns (with the three new columns containing something else), or to display the existing records in six columns rather than three? I.e., do you want this:
This would do it. There are a few points to consider, though:
[list][]Creating two sets of columns with identical content types makes the table harder to read.
[]Don’t use alt="Image" or something similar. The alt attribute should describe the image’s contents. If it’s already sufficiently described in the description, you should not add any contents to the alt attribute.
[]You should look into CSS to style your table.
[]You should also consider using a template, rather than mixing HTML with your PHP.[/list]
I’m sure you’ll agree that this is much easier to read and maintain.
(Note: This should work, but I haven’t been able to run the code, so there might be some typos).
Just an afterthought: This will throw an error for an odd number of results. You can fix this by either adding an extra, blank result after the foreach, or by setting up a break in the for.
big thanks there works ok
havnt used the template yet
have 1 prob odd number results show box with x in it guess this is what you ment
but dont know how to set that up
Just an afterthought: This will throw an error for an odd number of results. You can fix this by either adding an extra, blank result after the foreach, or by setting up a break in the for
Unless the output is way, way, way far away from the query, I’d not waste the overhead of a separate array on the data.
So far the methods presented crash if there’s an uneven number of elements present.
table formatting like this is easy if you use a counter INDEPENDANT of the key, since you shouldn’t trust array keys as odd/even indexes – which opens the door to using foreach.
echo '
<table>
<colgroup span="3"></colgroup>
<colgroup span="3"></colgroup>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Description</th>
<th scope="col">Photo</th>
<th scope="col">Item</th>
<th scope="col">Description</th>
<th scope="col">Photo</th>
</tr>
</thead><tbody>
'; // extra tabs and line make first TR line up right!
while ($row=mysql_fetch_assoc($result)) {
$count=0;
foreach ($row as $data) {
echo (
$count%2==0 ? '<tr>' : ''
),'
<td>',$data['item'],'</td>
<td>',$data['desc'],'</td>
<td>
<a href="http://www.b.net/test/pics/',$data['pname'],'">
<img
src="http://www.b.net/test/pics/',$data['pname'],'"
alt=""
width="100" height="100"
/>
</a>
</td>
',(
($count++)%2==0 ? '</tr>' : ''
);
}
}
if ($count%2==1) {
echo '
<td colspan="3"></td>
</tr>';
}
echo '
</tbody>
</table>';
Handles an uneven count of them properly, and is a bit simpler on the code.
Oh, and lands sake, ease up on the string additions in your echo’s – they should only be used when you HAVE to since that means building the entire string as a separate variable in memory, as opposed to using comma’s where it’s sent as a delimited list of pointers to the statics and vars. Doing one massive string addition before the send is a waste of memory and processing time. (might as well go back to doing the double quote nonsense at that point)
Hope this helps.
– edit –
Oh, and I’d be tempted to make that first TD a th with scope=“row” if that’s the item name. The second TH of the same scope will say what comes after is the start of a new section, doing more to semantically say they’re different sections than the colgroups up top would.
Doug
You’d place it where the previous code was placed. And no need for donations, I’m just here to help. Better spend the money in the SitePoint bookstore
Jason
Using the remainder is definitely a better method, especially if the number of column groups has to be changed later. To combine this with a template approach would be something like:
Wow, nothing like banging the brakes with all those slow string replacements… But then there’s a reason I don’t like “templating systems” when you have access to perfectly good functions/methods/variables… and why every time I deal with garbage like Smarty I walk away in disgust screaming “Oh for {expletive omitted} sake, can’t I just echo this so I have some blasted control?!?”
But to put that in perspective – I’m the old-school programmer who has always hated printf, even in C so…
I would say for certain that about 99.9% of the people heer would advise breaking up the code into logical components as C. Ankerstjerne did. Maybe not using a template system. None the less, breaking things up rather than using spaghetti coding it all into a single file will make it much easier to maintain, if its not fire and forget. Than again I love MVC… Though it seems that where dougvc is at perhaps adding more complexity isn’t the best approach. Breaking things up and following patterns isn’t really something you should be doing until you have a full grasp of a language.
Oh, and your logic flow is busted – you’re not outputting the empty final to finish the table, which can cause cross-browser rendering issues as undeclared TD are NOT assumed. You’re also building up the size of ‘table_contents’ to an unneccessary extreme as you go – making each str_replace slower than the last.
If you instead built your field, added it to a row every other flushing the field and adding to the data, then stuff the data in the table once at the END… Well…
Smaller memory footprint and executes several times faster (as the strings being parsed are kept small)… though still slow as molasses compared to echo – which is why I’d use SHOCK a function based template system instead of the string manipulation nonsense.
Jason
As I mentioned, it’s a very crude approach. I wrote it more or less in one go, just to illustrate how it would work. The template system I use for my sites are indeed based on a template class, and considerably faster than the code I wrote above. I just didn’t want to overly complicate the code for the original poster by introducing OOP into the mix.
And you are right, it should have been if($count%2) after the while loop.