ilifenets Posted August 20, 2010 Share Posted August 20, 2010 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> Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 20, 2010 Share Posted August 20, 2010 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); } Quote Link to comment Share on other sites More sharing options...
ilifenets Posted August 20, 2010 Author Share Posted August 20, 2010 Wow! Sign me up. That put us closer, so how do we make sure we have more than one row to display them? http://www.ilocalize.com/browse.php (your function used here) Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 20, 2010 Share Posted August 20, 2010 Wow! Sign me up. That put us closer, so how do we make sure we have more than one row to display them? I don't follow you. Quote Link to comment Share on other sites More sharing options...
ilifenets Posted August 20, 2010 Author Share Posted August 20, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 20, 2010 Share Posted August 20, 2010 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 Quote Link to comment Share on other sites More sharing options...
ilifenets Posted August 20, 2010 Author Share Posted August 20, 2010 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... Quote Link to comment Share on other sites More sharing options...
sasa Posted August 21, 2010 Share Posted August 21, 2010 change this lines $loop = (int)(count($vendors_states) / 5); if(count($vendors_states) % 5 > 0) $loop++; with $loop = count($vendors_states); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.