Jump to content

PHP LOOP rows to display... Modulus


ilifenets

Recommended Posts

I can't seem to get the math right!

 

I have a function, pulling 13 states (canadian provinces) from the database and I can't seem to get them all to show in 5 columns. Where am I doing this wrong. The code below only shows 7 of 13... I just can't seem to get where I am off!

 

PHP CODE:

function map_showCA()
{
	global $db, $tpl;

		// ShowLocalResources

	$sSQL = "SELECT * FROM states_canada ORDER BY state;";
	$db->query($sSQL);
	$next_record = $db->next_record();

	while ($next_record)
	{
		$state_id = $db->f("id");
		$state = $db->f("state");
		$statename = $db->f("statename");
		$statename_html = $db->f("statename_html");

		$next_record = $db->next_record();
		if (!USE_SEO_LINKS) { $statename_html = $state_id; }
		$vendors_states[] = array("name" => $statename, "id" => $state_id, "state_html" => $statename_html);
	}

	$vendors_states_col = array_chunk($vendors_states, 5, true);

	$loop = (int)(count($vendors_states) / 5);
	if(count($vendors_states) % 5 > 0) $loop++;

	$j = 0;

	for($i = 0; $i < $loop; $i++)
	{
		$parameters = 'http://'.$vendors_states_col[0][$j]["state_html"].'.ilocalize.com';
		// $first_column_link = generate_vendor_seo_url($parameters);
		$tpl->set_var("first_column_link", $parameters);
		$tpl->set_var("first_column", tohtml($vendors_states_col[0][$j]["name"]));

		$parameters = 'http://'.$vendors_states_col[1][$j+6]["state_html"].'.ilocalize.com';
		// $second_column_link = generate_vendor_seo_url($parameters);
		$tpl->set_var("second_column_link", $parameters);
		$tpl->set_var("second_column", tohtml($vendors_states_col[1][$j+6]["name"]));

		$parameters = 'http://'.$vendors_states_col[2][$j+12]["state_html"].'.ilocalize.com';
		// $threeth_column_link = generate_vendor_seo_url($parameters);
		$tpl->set_var("threeth_column_link", $parameters);
		$tpl->set_var("threeth_column", tohtml($vendors_states_col[2][$j+12]["name"]));

		$parameters = 'http://'.$vendors_states_col[3][$j+18]["state_html"].'.ilocalize.com';
		// $fourth_column_link = generate_vendor_seo_url($parameters);
		$tpl->set_var("fourth_column_link", $parameters);
		$tpl->set_var("fourth_column", tohtml($vendors_states_col[3][$j+18]["name"]));

		$parameters = 'http://'.$vendors_states_col[4][$j+24]["state_html"].'.ilocalize.com';
		// $fifth_column_link = generate_vendor_seo_url($parameters);
		$tpl->set_var("fifth_column_link", $parameters);
		$tpl->set_var("fifth_column", tohtml($vendors_states_col[4][$j+24]["name"]));

		$tpl->parse("ShowLocalResourcesCA", true);
		$j++;
	}
}

 

HTML CODE:

 <table border="0" cellpadding="1" cellspacing="1"  class="description_text2"  style="padding-bottom:6px;" width="100%" id="dashed_box_gray">
             <!--BeginShowLocalResourcesCA-->
                <tr>
                    <td width="110" align="left" style="padding-left:6px; padding:6px;"><a href="{first_column_link}" class="user_blue_14px">{first_column}</a></td>
                    <td width="109" align="left" style="padding-right:6px; padding:6px;"><a href="{second_column_link}" class="user_blue_14px">{second_column}</a></td>
                    <td width="109" align="left" style="padding-right:6px; padding:6px;"><a href="{threeth_column_link}" class="user_blue_14px">{threeth_column}</a></td>
                    <td width="109" align="left" style="padding-right:6px; padding:6px;"><a href="{fourth_column_link}" class="user_blue_14px">{fourth_column}</a></td>
                    <td width="108" align="left" style="padding-right:6px; padding:6px;"><a href="{fifth_column_link}" class="user_blue_14px">{fifth_column}</a></td>
                </tr>
                <!--EndShowLocalResourcesCA-->
                </table>

Link to comment
Share on other sites

The problem is your use of $next_record. You call it in the while loop AND inside the loop itself. So each iteration of the loop will use up two records. I think this is a more efficient method:

function map_showCA()
{
    global $db, $tpl;
// ShowLocalResources
    $sSQL = "SELECT id, state, statename, statename_html FROM states_canada ORDER BY state;";
    $db->query($sSQL);
    
    $recordIdx = 0;
    $column_names = array('first', 'second', 'threeth', 'fourth', 'fifth');
    
    while ($db->next_record())
    {
        $state = $db->f("state");
        $statename = $db->f("statename");
        $statename_html = (!USE_SEO_LINKS) ? $statename_html = $db->f("id") : $db->f("statename_html");
        
        //Determine the column and column name
        $colIdx = $recordIdx%count($column_names);
        $col_name = $column_names[$colIdx];
        
        $parameters = "http://{$statename_html}ilocalize.com";
        // $column_link = generate_vendor_seo_url($parameters);
        $tpl->set_var("{$col_name}_column_link", $parameters);
        $tpl->set_var("{$col_name}_column", tohtml($statename));
        
        $recordIdx++;
    }
    
    $tpl->parse("ShowLocalResourcesCA", true);
}

Link to comment
Share on other sites

Well, the code you gave works great, much simpler, but we still don't get all 13 provinces in the canada table....

 

ie.

http://www.ilocalize.com/browse.php

we only get 5, instead of 13, so how do we use your good code example but getting new rows and listing them alphabetically? Like the first table on that sample shows the STATES in the U.S. alphabetically in rows.

Link to comment
Share on other sites

Hmm.. you are using a class to create the actual HTML and I made some assumptions about what the class code is doing. It's kind of hard for me to really provide a solution without knowing what those functions do - I just made a guess.

 

Since it is still only showing 5 I will make another *guess* that the $tpl->parse() method is hard coded to only process 5 records. If that is the case, this *might* work:

 

<?php

function map_showCA()
{
    global $db, $tpl;

// ShowLocalResources
    $sSQL = "SELECT id, state, statename, statename_html FROM states_canada ORDER BY state;";
    $db->query($sSQL);
    
    $recordIdx = 0;
    $records_per_column = 5;
    $column_names = array('first', 'second', 'threeth', 'fourth', 'fifth');
    
    while ($db->next_record())
    {
        $state = $db->f("state");
        $statename = $db->f("statename");
        $statename_html = (!USE_SEO_LINKS) ? $statename_html = $db->f("id") : $db->f("statename_html");
        
        //Determine the column and column name
        $colIdx = $recordIdx%count($column_names);
        $col_name = $column_names[$colIdx];
        
        $parameters = "http://{$statename_html}ilocalize.com";
        // $column_link = generate_vendor_seo_url($parameters);
        $tpl->set_var("{$col_name}_column_link", $parameters);
        $tpl->set_var("{$col_name}_column", tohtml($statename));
        $recordIdx++;
        
        //After 5 records create row
        if($recordIdx%$records_per_column==0)
        {
            $tpl->parse("ShowLocalResourcesCA", true);
        }

    }
    
    //If any remainginrecords create last row
    if($recordIdx%$records_per_column>0)
    {
        $tpl->parse("ShowLocalResourcesCA", true);
    }
}
?>

 

if it doesn't work I'd need to know what that class is doing

 

 

Link to comment
Share on other sites

Thanks again. No, it isn't the class, parse is just parsing the data in the original code's for loop and displaying it between two html tags in html file...

 

that is why the for loop was necessary in our code... I'll play around with it, so far, no luck as of yet though...

Link to comment
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.