PDA

View Full Version : Row count


Mythotical
20 Nov 2009, 21:12
Did anything change how row count is done?

$sql = $vbulletin->db->query_read("SELECT * FROM `" . TABLE_PREFIX . "download_cats` WHERE cat_active = '1'");

$total = $vbulletin->db->num_rows($sql);
$i=0;
$perrow = $total-2;

while ($row = $db->fetch_array($sql))
{
$i++;

if ($total !== 0)
{

if ($total > $perrow){
$catc = '</td>';
$perrow = $perrow+2;
} else {
$perrow = $perrow-2;
$catc = '</td></tr><tr>';
}


I'm not sure if that is the problem but when I refresh my page is keeps displaying:
</td></td></tr><tr>

Not sure if its the row count or not. Hard to say right now.

Thanks
Steve

Lynne
20 Nov 2009, 22:00
num_rows is still num_rows. You are missing an end } to your code though.

Mythotical
20 Nov 2009, 22:12
Nah its there, here is the full snippet:
$sql = $vbulletin->db->query_read("SELECT * FROM `" . TABLE_PREFIX . "download_cats` WHERE cat_active = '1'");

$total = $vbulletin->db->num_rows($sql);
$i=0;
$perrow = $total-2;

while ($row = $db->fetch_array($sql))
{
$cid = $row['catid'];
$cat = $row['catitle'];
$description = $row['cat_description'];
$caticon = $row['cat_icon'];
$i++;

if ($total !== 0)
{

if ($total > $perrow){
$catc = '</td>';
$perrow = $perrow+2;
} else {
$perrow = $perrow-2;
$catc = '</td></tr><tr>';
}

$cat_data = vB_Template::create('bfc_download_cat_bit');
$cat_data->register('cid', $cid);
$cat_data->register('cat', $cat);
$cat_data->register('description', $description);
$cat_data->register('caticon', $caticon);
$cat_data->register('catc', $catc);
$cat_data->register('cat_bit', $cat_bit);
$cat_bit .= $cat_data->render();
}

}

Lynne
20 Nov 2009, 22:42
If you are just getting "</td></td></tr><tr>" all the time, then perhaps your template is incorrect. I assume you want something else to be input in there which is missing?

Mythotical
20 Nov 2009, 22:57
If you look at the code I have it set that if there are 2 cats to display then it will and it will add </td></tr><tr> then start a new row but if their is only one it will not throw that out yet, it will use </td> only instead.

Lynne
20 Nov 2009, 23:06
Yeah, I get that. perhaps for debugging purposes, you should be spitting $perrow out also to see what it is set to.

Adrian Schneider
20 Nov 2009, 23:11
Just a suggestion:
Put each cell into an array, and then use array_chunk to get your groups. Much more solid than messing around with counters and stuff.

Mythotical
21 Nov 2009, 00:38
Just a suggestion:
Put each cell into an array, and then use array_chunk to get your groups. Much more solid than messing around with counters and stuff.

Um....how do you mean? Got an example or somewhere I can look at an example? I'm a visual person, visual aids help me more to do code than just being told.

Cheers
Steve

Adrian Schneider
21 Nov 2009, 00:52
Here's a sample $columns = 5;
$width = floor(100 / $columns);

// generate $cells array containing each HTML cell
$cells = array();
while ($row = $db->fetch_array($result)) {
$cells[] = '<td style="width: ' . $width . '%">HTML For the Cell</td>';
}

// pad with empty cells until last row would be full
while (count($prepared) % $columns) {
$prepared[] = '<td style="width: ' . $width . '%"></td>';
}

// group into rows with variable number of columns
$rows = '';
foreach (array_chunk($prepared, $columns) as $row) {
$rows .= '<tr>' . implode(' ', $row) . '</tr>';
}

// done
return '<table border="0" width="100%">' . $rows . '</table>';

Mythotical
21 Nov 2009, 01:02
Here's a sample $columns = 5;
$width = floor(100 / $columns);

// generate $cells array containing each HTML cell
$cells = array();
while ($row = $db->fetch_array($result)) {
$cells[] = '<td style="width: ' . $width . '%">HTML For the Cell</td>';
}

// pad with empty cells until last row would be full
while (count($prepared) % $columns) {
$prepared[] = '<td style="width: ' . $width . '%"></td>';
}

// group into rows with variable number of columns
$rows = '';
foreach (array_chunk($prepared, $columns) as $row) {
$rows .= '<tr>' . implode(' ', $row) . '</tr>';
}

// done
return '<table border="0" width="100%">' . $rows . '</table>';

Ahhhhh, I see what you mean now, thank you very much. Never fails you come to my rescue on things. LOL

I'll have to give it a try tomorrow, heading out for a night on the town with the wife.

--------------- Added 1258848871 at 1258848871 ---------------

BTW, I figured out that I was using wrong code in the template:
{vb:var catc}

But once I changed to:
{vb:raw catc}

It all worked out, the code appearing at top went away so now I just need to convert to Adrian's suggestion and it will be good to go.