Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: The Wizard - tammikuu 07, 2014, 10:40:13 AP

Otsikko: Pagination Issue
Kirjoitti: The Wizard - tammikuu 07, 2014, 10:40:13 AP
Hello:

I am having problems getting the pagination to work on this part of my script.
Here is the set up - in the database I have a column called wizard and each user has a cell.
The information inside the cell is like this -  doctor01, doctor02, doctor03, ect...
I have added a few echos in the template to test and see if the data is getting through and what it looks like.
Here are the latest results -
Item Count:19
Items per Page:12
The page count:2
Items:Array

Right now I am not seeing any of the png images in the template and all it is showing is this - Item Image one time and Pages: < Back 1 2 Next >.

The result I am trying to achieve is to have 12 images per page.

Can anyone solve this problem?

Wiz


WizardsShopUser.template.php

function items_on_display()
{
global $txt, $context, $modSettings, $boardurl, $scripturl;

echo '
<div class="cat_bar">
<h3 class="catbg" align="center">', $txt['wizards_shop_display'], '</h3>
</div>
<div class="windowbg">
<span class="topslice"><span></span></span>
<div class="content" style="height:auto;min-height:', $context['wizards_shop_box_height'],'px;">
';

echo 'Item Count:';
echo $context['shop_inv']['item_count'];
echo '<br>Items per Page:';
echo $modSettings['shopItemsPerPage'];
echo '<br>The page count:';
echo $context['shop_inv']['pages']['total'];
echo '<br>Items:';
echo $context['shop_inv']['items'];

// Show all Items

$i = 0;

echo '
<center>
<table class="wizards_shop_5px_padding">
<tr valign="top">
';

foreach ($context['shop_inv']['items'] as $item)
{
if (empty($item))
continue;

$i++;

echo '
<td align="left" valign="top">
<img width="', $modSettings['shopImageWidth'], '" height="', $modSettings['shopImageHeight'], '" src="', $boardurl, '/Themes/default/wizards-shop/small/' . $item['image'] . '" align="left" alt="Item Image" />
</td>
<td>
';

$wiz = 3;

if ($i % $wiz == 0)
echo '
</tr>
';
}

// If this listing should have paging at the bottom
if (isset($context['shop_inv']['pages']['current']) && isset($context['shop_inv']['pages']['total']))
{
echo '
<center>
<tr>
<td colspan="6" align="center"><strong>Pages: </strong>';

//If current page != 1, show '< Back' link
if ($context['shop_inv']['pages']['current'] != 1)
{
$prevPage = $context['shop_inv']['pages']['current'] - 1;
echo '
<a href="', $context['shop_inv']['pages']['link'], ';page=', $prevPage, '">', $txt['wizards_shop_back'], '</a>';
}
else
echo '
', $txt['wizards_shop_back'], '';

// Show links to all pages
for ($x = 1; $x <= $context['shop_inv']['pages']['total']; $x++)
{
// If this number is the current page, don't make number a link
if ($x == $context['shop_inv']['pages']['current'])
echo '
<strong>', $x, '</strong>';
else
echo '
<a href="', $context['shop_inv']['pages']['link'], ';page=', $x, '">', $x, '</a> ';
}

// If current page != last page, show 'Next >' link
if ($context['shop_inv']['pages']['current'] != $context['shop_inv']['pages']['total'])
{
$nextPage = $context['shop_inv']['pages']['current'] + 1;
echo '
<a href="', $context['shop_inv']['pages']['link'], ';page=', $nextPage, '">', $txt['wizards_shop_next2'], '</a> ';
}
else
echo '
', $txt['wizards_shop_next2'], ' ';
}

echo '
</td>
</tr>
</table>
</center>
</div>
<span class="botslice"><span></span></span>
</div>
';
}



Subs-Wizards_Shop_User.php

function ShopDisplay()
{
global $smcFunc, $txt, $user_info, $context, $boardurl, $scripturl, $modSettings;

loadTemplate('WizardsShopUser');
loadLanguage('WizardsShop');

// Page title for the main page
$context['page_title'] = $txt['wizards_shop_display_page_title'];

// Navigational link tree to be shown at the top of the page.
$context['linktree'][] = array(
'url' => $scripturl . '?action=shop_display',
'name' => $txt['wizards_shop_display']
);

// Title Text
$context['shop_display_Head'] = $txt['wizards_shop_display'];

// We're using the "display" template
$context['sub_template'] = 'display';

// Show all Images user has on Display

$request1 = $smcFunc['db_query']('', '
SELECT wizard
FROM {db_prefix}members
WHERE id_member={int:current_member}         
LIMIT 1', array(
'current_member' => $user_info['id']
));

// Populate the array

while ($row = $smcFunc['db_fetch_assoc']($request1))
{
if (empty($row))
return false;

$wizard2 = explode(',', $row['wizard']);

} // End of While bracket

$context['shop_inv']['item_count'] = count($wizard2);
$smcFunc['db_free_result']($request1);

// If the page is not set, assume page 1
if (!isset($_GET['page']))
$_GET['page'] = 1;

// If items per page not set, assume 15
if (!isset($modSettings['shopItemsPerPage']))
$modSettings['shopItemsPerPage'] = 15;

// The page count - Total number of items divided by number per page
$context['shop_inv']['pages']['total'] = ceil($context['shop_inv']['item_count'] / $modSettings['shopItemsPerPage']);

$firstItem = ($_GET['page'] * $modSettings['shopItemsPerPage']) - $modSettings['shopItemsPerPage'];

// Get the user's information from the database

// Start with empty items array
$context['shop_inv']['items'] = array();

// Get all the inventory items on current page
$result = $smcFunc['db_query']('', '
SELECT wizard
FROM {db_prefix}members
WHERE id_member={int:current_member}         
LIMIT {int:firstitem}, {int:itemsperpage}', array(
'current_member' => $user_info['id'],
'firstitem' => $firstItem,
'itemsperpage' => $modSettings['shopItemsPerPage']
));

// Loop through results
while ($row = $smcFunc['db_fetch_assoc']($result))
$context['shop_inv']['items'][] = array(
'image' => $row['image'],
);
$smcFunc['db_free_result']($result);

// Set some miscellaneous variables
$context['shop_inv']['pages']['current'] = $_GET['page'];
$context['shop_inv']['pages']['link'] = $scripturl . '?action=shop_display;do=inv';

}

Otsikko: Re: Pagination Issue
Kirjoitti: margarett - tammikuu 07, 2014, 03:18:51 IP
Use print_r for the array instead of echo.

And use your other topic, why start a new one?
Otsikko: Re: Pagination Issue
Kirjoitti: The Wizard - tammikuu 07, 2014, 04:30:22 IP
LainaaAnd use your other topic, why start a new one?

I believe the other topic was incorrectly titled as my coding problem is a pagination issue and as a result less users believed they could help. By starting a new topic I hope more users will look over my code and maybe someone out there can solve the problem and is inclined to help.

I have been looking at the code off and on for a day or so and have tried a few ideas too no avail. I think I am close, but the solution is hiding in plane sight and I can't see it.

Wiz
Otsikko: Re: Pagination Issue
Kirjoitti: margarett - tammikuu 07, 2014, 08:46:18 IP
This
// Get all the inventory items on current page
$result = $smcFunc['db_query']('', '
SELECT wizard
FROM {db_prefix}members
WHERE id_member={int:current_member}         
LIMIT {int:firstitem}, {int:itemsperpage}', array(
'current_member' => $user_info['id'],
'firstitem' => $firstItem,
'itemsperpage' => $modSettings['shopItemsPerPage']
));

// Loop through results

Doesn't work. You are again fetching the field "wizard" for the current user. This, of course, will return just 1 row. In fact, this is pretty much the result of the 1st query ;) and that's why you are not seeing nothing at the moment.

This second query should be hitting the "items" table, so that it hits the results of the $wizard2 array. This, assuming that doctor01, doctor02, etc can be found in that table.
So your issue is really with fetching the correct data, and not pagination :P

Can you give us an insight of how your "inventory" and "items" tables look like? With values, if possible.
Otsikko: Re: Pagination Issue
Kirjoitti: The Wizard - tammikuu 08, 2014, 06:48:40 AP
LainaaThis second query should be hitting the "items" table, so that it hits the results of the $wizard2 array. This, assuming that doctor01, doctor02, etc can be found in that table.
So your issue is really with fetching the correct data, and not pagination :P

Can you give us an insight of how your "inventory" and "items" tables look like? With values, if possible.

Below are images of the database where the $wizard2 data comes from. I'm afraid I don't know the right coding vocabulary to explane, but hopefully the images will do the talking for me.

http://www.tribeuniverse3.com/temp5/image1.png

http://www.tribeuniverse3.com/temp5/image2.png
Otsikko: Re: Pagination Issue
Kirjoitti: margarett - tammikuu 08, 2014, 07:40:47 IP
No, that's "members" table ;)

Based on the code on your other topic (http://www.simplemachines.org/community/index.php?topic=516591.0), you have 2 tables:
{db_prefix}shop_inventory
{db_prefix}shop_items

Those are the ones I'd like to see some content. Not just the structure, some values also ;)
Please mask any sensitive information that may be shown (names, filenames, etc)
Otsikko: Re: Pagination Issue
Kirjoitti: The Wizard - tammikuu 10, 2014, 08:09:57 AP
Hello:

The following code below is a ruff draft of what I am trying to do. The code does work.

Now here is my question -
When you run the script the pagination links shows up at the top of the page before the data.
How can I make the pagination links show up at the bottom of the page?

Wiz


<?php

$request1 = array(doctor1doctor2doctor3doctor4doctor5doctor6doctor7doctor8doctor9doctor10);

//$context['shop_inv']['pages']['current'] = 1;

$context['shop_inv']['pages']['link'] = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$pt explode('&',$context['shop_inv']['pages']['link']);
if (strpos($context['shop_inv']['pages']['link'],'pageno'))
array_pop($pt);
$pt implode('&',$pt);
$context['shop_inv']['pages']['link'] = $pt;
$context['shop_inv']['items'] = $request1// REPLACE $KEY WITH YOUR ARRAY VARIABLE
$page $_REQUEST['pageno'];

$context['shop_inv']['pages']['current'] = isset($page) ? (integer)$page 1;

$modSettings['shopItemsPerPage'] = 6;

$z=0;

echo '
<center>
<table>
<tr valign="top">
'
;

$total count($context['shop_inv']['items']);
$context['shop_inv']['pages']['total'] = ceil($total $modSettings['shopItemsPerPage']); //TOTAL NUMBER OF PAGES

if(isset($context['shop_inv']['items']))
{
if (($context['shop_inv']['pages']['current'] > 0) && ($context['shop_inv']['pages']['current'] <= $context['shop_inv']['pages']['total']))
{
//STARTING LOOP FOR ARRAY DATA
$start = ($context['shop_inv']['pages']['current'] - 1) * $modSettings['shopItemsPerPage'];

for($i $start$i <=($modSettings['shopItemsPerPage'] + $start 1); $i++) 
{

$z++;

echo '<td>'$context['shop_inv']['items'][$i], '</td><br>';

$wiz 3;

if ($z $wiz == 0)
echo '
</tr>
'
;

}
}
}

// If this listing should have paging at the bottom
if (isset($context['shop_inv']['pages']['current']) && isset($context['shop_inv']['pages']['total']))
{
if ($context['shop_inv']['pages']['current'] != 1
//GOING BACK FROM PAGE 1 SHOULD NOT BET ALLOWED
$previous_page $context['shop_inv']['pages']['current'] - 1;
$previous '<a href="'$context['shop_inv']['pages']['link'] .'?pageno='.$previous_page.'"> <</a> ';    
}

$pages '';
for ($a 1$a <= $context['shop_inv']['pages']['total']; $a++)
{
if ($a == $context['shop_inv']['pages']['current']) 
$pages .= $a .'</u> ';
else 
$pages .= '<a href="'$context['shop_inv']['pages']['link'] .'?pageno='.$a.'" >'$a .'</a> ';
}

$pages substr($pages,0,-1); //REMOVING THE LAST COMMA (,)

if ($context['shop_inv']['pages']['current'] != $context['shop_inv']['pages']['total']) 
//GOING AHEAD OF LAST PAGE SHOULD NOT BE ALLOWED
$next_page $context['shop_inv']['pages']['current'] + 1;
$next ' <a href="'$context['shop_inv']['pages']['link'] .'?pageno='.$next_page.'"> ></a>';
}

echo '

'
$previous $pages $next//PAGINATION LINKS
}

echo '
</td>
</tr>
</table>
</center>
</div>
<span class="botslice"><span></span></span>
</div>
'
;
Otsikko: Re: Pagination Issue
Kirjoitti: The Wizard - tammikuu 10, 2014, 09:27:41 AP
Just a quick note: I have hopefully solved this issue and will post my completed code later today and then marked the topic as solved

Thanks for all the help

Wiz
Otsikko: Re: Pagination Issue
Kirjoitti: The Wizard - tammikuu 10, 2014, 12:40:38 IP
Well here is the completed code -

WizardsShopUser.template.php

function items_on_display()
{
global $txt, $context, $modSettings, $boardurl, $scripturl;

echo '
<div class="cat_bar">
<h3 class="catbg" align="center">', $txt['wizards_shop_display'], '</h3>
</div>
<div class="windowbg">
<span class="topslice"><span></span></span>
<div class="content" style="height:auto;min-height:', $context['wizards_shop_box_height'],'px;">
';

$z=0;

echo '
<center>
<table class="wizards_shop_5px_padding">
<tr valign="top">
';

if(isset($context['shop_inv']['items']))
{
if (($context['shop_inv']['pages']['current'] > 0) && ($context['shop_inv']['pages']['current'] <= $context['shop_inv']['pages']['total']))
{
//STARTING LOOP FOR ARRAY DATA
$start = ($context['shop_inv']['pages']['current'] - 1) * $modSettings['shopItemsPerPage'];

for($i = $start; $i <=($modSettings['shopItemsPerPage'] + $start - 1); $i++)
{
if (empty($context['shop_inv']['items'][$i]))
continue;

$z++;

echo '
<td>
<img width="', $modSettings['shopImageWidth'], '" height="', $modSettings['shopImageHeight'], '" src="', $boardurl, '/Themes/default/wizards-shop/small/' , $context['shop_inv']['items'][$i], '" align="left" alt="Item Image" />
</td>';

$wiz = 3;

if ($z % $wiz == 0)
echo '
</tr>
';

}
}
}

// If this listing should have paging at the bottom
if (isset($context['shop_inv']['pages']['current']) && isset($context['shop_inv']['pages']['total']))
{
echo '
<center>
<tr>
<td colspan="6" align="center"><strong>Pages: </strong>';
}

// If this listing should have paging at the bottom
if (isset($context['shop_inv']['pages']['current']) && isset($context['shop_inv']['pages']['total']))
{
if ($context['shop_inv']['pages']['current'] != 1)
{ //GOING BACK FROM PAGE 1 SHOULD NOT BET ALLOWED
$previous_page = $context['shop_inv']['pages']['current'] - 1;
$previous = '<a href="'. $context['shop_inv']['pages']['link'] .';pageno='.$previous_page.'"> <</a> ';   
}

$pages = '';
for ($a = 1; $a <= $context['shop_inv']['pages']['total']; $a++)
{
if ($a == $context['shop_inv']['pages']['current'])
$pages .= $a .'</u> ';
else
$pages .= '<a href="'. $context['shop_inv']['pages']['link'] .';pageno='.$a.'" >'. $a .'</a> ';
}

$pages = substr($pages,0,-1); //REMOVING THE LAST COMMA (,)

if ($context['shop_inv']['pages']['current'] != $context['shop_inv']['pages']['total'])
{ //GOING AHEAD OF LAST PAGE SHOULD NOT BE ALLOWED
$next_page = $context['shop_inv']['pages']['current'] + 1;
$next = ' <a href="'. $context['shop_inv']['pages']['link'] .';pageno='.$next_page.'"> ></a>';
}

echo '

'. $previous . $pages . $next; //PAGINATION LINKS
}

echo '
</td>
</tr>
</table>
</center>
</div>
<span class="botslice"><span></span></span>
</div>
';
}



Subs-Wizards_Shop_User.php

function ShopDisplay()
{
global $smcFunc, $txt, $user_info, $context, $boardurl, $scripturl, $modSettings;

loadTemplate('WizardsShopUser');
loadLanguage('WizardsShop');

// Page title for the main page
$context['page_title'] = $txt['wizards_shop_display_page_title'];

// Navigational link tree to be shown at the top of the page.
$context['linktree'][] = array(
'url' => $scripturl . '?action=shop_display',
'name' => $txt['wizards_shop_display']
);

// Title Text
$context['shop_display_Head'] = $txt['wizards_shop_display'];

// We're using the "display" template
$context['sub_template'] = 'display';

/////////////////////START OF ARRAY PAGINATION CODE/////////////////////

$request1 = $smcFunc['db_query']('', "
SELECT wizard
FROM {db_prefix}members
WHERE id_member = {int:current_member}
LIMIT 1", array(
'current_member' => $user_info['id']
));

// Populate the array

while ($row = $smcFunc['db_fetch_assoc']($request1))
{
if (empty($row))
return false;

$wizard2 = explode(',', $row['wizard']);

} // End of While bracket

//$context['shop_inv']['pages']['link'] = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$context['shop_inv']['pages']['link'] = $scripturl . '?action=shop_display';
$pt = explode('&',$context['shop_inv']['pages']['link']);
if (strpos($context['shop_inv']['pages']['link'],'pageno'))
array_pop($pt);
$pt = implode('&',$pt);
$context['shop_inv']['pages']['link'] = $pt;
$context['shop_inv']['items'] = $wizard2; // REPLACE $KEY WITH YOUR ARRAY VARIABLE
$page = $_REQUEST['pageno'];

$context['shop_inv']['pages']['current'] = isset($page) ? (integer)$page : 1;

$total = count($context['shop_inv']['items']);
$context['shop_inv']['pages']['total'] = ceil($total / $modSettings['shopItemsPerPage']); //TOTAL NUMBER OF PAGES

}