Monkuar Posted February 2, 2012 Share Posted February 2, 2012 I have a $counter++ it goes inside my while loop but it echo's out 12345 instead of the amount of the all of them? (like 1 2 3 4 5 should echo out 5.. not 12345 ? any help? i think my brain farting atm lol i even tried to wrap count around it, doesnt wokr Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/ Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 im not sure that im following you. you want $counter to contain the sum of the iterations? Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313519 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 im not sure that im following you. you want $counter to contain the sum of the iterations? Yes, when i Do it, it echo's 1234567 but how can I get just "7" out of the loop? i need to use it in a variable i dont need to use the 1234567 just need the last number, or the sum of all which is 7 Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313520 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 im not sure that im following you. you want $counter to contain the sum of the iterations? Yes, when i Do it, it echo's 1234567 but how can I get just "7" out of the loop? i need to use it in a variable i dont need to use the 1234567 just need the last number, or the sum of all which is 7 last time i checked, 1+2+3+4+5+6+7 != 7, but maybe something changed. okay so you want to only echo the last iteration, or store it in a variable. Can you post the loop so I can have a look? Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313521 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 im not sure that im following you. you want $counter to contain the sum of the iterations? Yes, when i Do it, it echo's 1234567 but how can I get just "7" out of the loop? i need to use it in a variable i dont need to use the 1234567 just need the last number, or the sum of all which is 7 last time i checked, 1+2+3+4+5+6+7 != 7, but maybe something changed. okay so you want to only echo the last iteration, or store it in a variable. Can you post the loop so I can have a look? Yea i just need to get the last number, lol my bad. while( $topic = $DB->fetch_row() ) { $counter++; { the query just seleects some dumb mysql data for testing I just need need to echo out the last number in the $counter++, I know 1234567 = 7 numbers, but I am doing a if statement if ($counter > 4){ $this->output .= <<< E </table></dd> E; } and I need to get my 4 to a different variable so it matches my database, it's really confusing atm, but if I can just get the $counter to show the last number instead of 1234567 it would work my code is to big/crap to post all of it it's confusing as is Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313522 Share on other sites More sharing options...
scootstah Posted February 2, 2012 Share Posted February 2, 2012 $counter = 1; while($topic = $DB->fetch_row()) { if ($counter == $DB->num-rows()) { echo $counter; } $counter++; } Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313523 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 alright, since your loop is grabbing database data as I expected, I will post dummy code that you can use to get the idea and implement into your code. $index = 1; while($row = $DB->fetch_row()) { //code if($index == $DB->num_rows()) //this indicates the last iteration { $last_iteration = $index; //stores the last iteration value } $index++; } Edit: or scootstah could vulture this thread. Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313524 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 LOOP $cats++; LOOP my code echo substr($cats, -1); Does not display my last number? This is getting retarded, the substr code on php.net says it display the LAST NUMBER/STRING using the -1, and it does not. your guys code not working because the num_rows is to broad, hence im only counter if the data is a Category of a forum, not all the data Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313525 Share on other sites More sharing options...
scootstah Posted February 2, 2012 Share Posted February 2, 2012 LOOP $cats++; LOOP my code echo substr($cats, -1); Does not display my last number? This is getting retarded, the substr code on php.net says it display the LAST NUMBER/STRING using the -1, and it does not. your guys code not working because the num_rows is to broad, hence im only counter if the data is a Category of a forum, not all the data substr removes a portion of a string, it has nothing to do with what you are trying to do. Unless you actually post code we can't really help you any further. Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313527 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 LOOP $cats++; LOOP my code echo substr($cats, -1); Does not display my last number? This is getting retarded, the substr code on php.net says it display the LAST NUMBER/STRING using the -1, and it does not. your guys code not working because the num_rows is to broad, hence im only counter if the data is a Category of a forum, not all the data substr removes a portion of a string, it has nothing to do with what you are trying to do. Unless you actually post code we can't really help you any further. ok fine be warned it's bad $DB->query("SELECT s.trid, s.member_id, s.topic_id, s.last_sent, s.start_date as track_started, t.*, f.id as forum_id, f.name as forum_name, f.read_perms " ."FROM ibf_tracker s, ibf_topics t, ibf_forums f " ."WHERE s.member_id='".$this->member['id']."' AND t.tid=s.topic_id AND f.id=t.forum_id " ."ORDER BY f.id, t.last_post DESC"); if ( $DB->get_num_rows() ) { $last_forum_id = -1; while( $topic = $DB->fetch_row() ) { $counter++; if ($last_forum_id != $topic['forum_id']) { $cats++; $last_forum_id = $topic['forum_id']; $this->output .= <<< E <td colspan=10><dl><dt><a href=?z={$topic['forum_id']}>{$topic['forum_name']}</a></dt><dd> <table class="tuble"> 25 E; } $topic['last_poster'] = ($topic['last_poster_id'] != 0) ? "<b><a href='{$this->base_url}?i=03&MID={$topic['last_poster_id']}'>{$topic['last_poster_name']}</a></b>" : "-".$topic['last_poster_name']."-"; $topic['starter'] = ($topic['starter_id'] != 0) ? "<a href='{$this->base_url}?i=03&MID={$topic['starter_id']}'>{$topic['starter_name']}</a>" : "-".$topic['starter_name']."-"; if ($topic['poll_state']) { $topic['prefix'] = $ibforums->vars['pre_polls'].' '; } $topic['folder_icon'] = $std->folder_icon($topic); $topic['topic_icon'] = $topic['icon_id'] ? '<img src="'.$ibforums->vars[html_url] . '/icon' . $topic['icon_id'] . '.gif" border="0" alt="">' : ' '; if ($topic['pinned']) { $topic['topic_icon'] = "<{B_PIN}>"; } $topic['start_date'] = $std->get_date( $topic['track_started'], 'LONG' ); if ($topic['description']) { $topic['description'] = $topic['description'].'<br>'; } $pages = 1; if ($topic['posts']) { if ( (($topic['posts'] + 1) % $ibforums->vars['display_max_posts']) == 0 ) { $pages = ($topic['posts'] + 1) / $ibforums->vars['display_max_posts']; } else { $number = ( ($topic['posts'] + 1) / $ibforums->vars['display_max_posts'] ); $pages = ceil( $number); } } if ($pages > 1) { $topic['PAGES'] = "<span class='small'>({$ibforums->lang['topic_sp_pages']} "; for ($i = 0 ; $i < $pages ; ++$i ) { $real_no = $i * $ibforums->vars['display_max_posts']; $page_no = $i + 1; if ($page_no == 4) { $topic['PAGES'] .= "<a href='{$this->base_url}a=ST&f={$this->forum['id']}&t={$topic['tid']}&st=" . ($pages - 1) * $ibforums->vars['display_max_posts'] . "'>...$pages </a>"; break; } else { $topic['PAGES'] .= "<a href='{$this->base_url}a=ST&f={$this->forum['id']}&t={$topic['tid']}&st=$real_no'>$page_no </a>"; } } $topic['PAGES'] .= ")</span>"; } if ($topic['posts'] < 0) $topic['posts'] = 0; // Do the quick goto last page icon stuff $topic['last_post_date'] = $std->get_date( $topic['last_post'], 'LONG' ); $this->output .= $this->html->subs_row($topic); echo 4; if ($counter > 4){ $this->output .= <<< E </table></dd> E; } } } else { $this->output .= $this->html->subs_none(); } I need the $counter to be > 4 and 4 has to be dynamic based on the content generated, because the </table> and </dd> shows the end of the category per row, it's confusing but the code: if ($last_forum_id != $topic['forum_id']) { $cats++; $last_forum_id = $topic['forum_id']; $this->output .= <<< E <td colspan=10><dl><dt><a href=?z={$topic['forum_id']}>{$topic['forum_name']}</a></dt><dd> <table class="tuble"> 25 E; } is what shows each Category, then the forum data under neath it, but the stupid part is I cant even copy paste this and use it at the bottom? it's retarded, I have to use some counter thing, really pissing me off Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313528 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 All I need is to get the amount of Categorys... .the user has and use it in my while loop, but i cant Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313529 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 LOOP $cats++; LOOP my code echo substr($cats, -1); Does not display my last number? This is getting retarded, the substr code on php.net says it display the LAST NUMBER/STRING using the -1, and it does not. your guys code not working because the num_rows is to broad, hence im only counter if the data is a Category of a forum, not all the data i see what you are trying to do, the code that we have already provided is what you need, attempt to implement it into your code. But, to answer your question on why substr() isn't working. 1. $cats will not contain 1234567 when echoed after the loop is finished 2. substr() expects parameter 1 to be type string, $cats is type int 3. if you want the last iteration value outside of the while loop, $counter will hold that value outside of the loop, if you want to echo something on the last iteration, refer to the code already given. Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313531 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 LOOP $cats++; LOOP my code echo substr($cats, -1); Does not display my last number? This is getting retarded, the substr code on php.net says it display the LAST NUMBER/STRING using the -1, and it does not. your guys code not working because the num_rows is to broad, hence im only counter if the data is a Category of a forum, not all the data i see what you are trying to do, the code that we have already provided is what you need, attempt to implement it into your code. But, to answer your question on why substr() isn't working. 1. $cats will not contain 1234567 when echoed after the loop is finished 2. substr() expects parameter 1 to be type string, $cats is type int 3. if you want the last iteration value outside of the while loop, $counter will hold that value outside of the loop, if you want to echo something on the last iteration, refer to the code already given. if($index == $DB->get_num_rows()) //this indicates the last iteration { $this->output .= <<< E </table></dd> E; $last_iteration = $index; //stores the last iteration value } i put in $index = 1; ontop of my loop that doesn't seem true and it returns false Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313533 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 just reviewed your code, im confused as to why you stated that the similar code that scootstah and I provided would not work for you, as the solution is basically the same. This is a modified snippet of your code. if ( $DB->get_num_rows() ) { $last_forum_id = -1; while( $topic = $DB->fetch_row() ) { $counter++; //$counter should begin with a value of 1, $counter++ should be placed at the bottom of the loop if($counter == $DB->get_num_rows()) //indicates the last iteration { //code to execute on the last iteration } Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313534 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 just reviewed your code, im confused as to why you stated that the similar code that scootstah and I provided would not work for you, as the solution is basically the same. This is a modified snippet of your code. if ( $DB->get_num_rows() ) { $last_forum_id = -1; while( $topic = $DB->fetch_row() ) { $counter++; //$counter should begin with a value of 1, $counter++ should be placed at the bottom of the loop if($counter == $DB->get_num_rows()) //indicates the last iteration { //code to execute on the last iteration } im not explaining myself ebtter $this->output .= $this->html->subs_row($topic); if ($counter > 4){ $this->output .= <<< E </table></dd> E; } this needs to be outputted inside the loop bro, I CANNOT use this outside of my LOOP.... if i did, it doesn't display right.. the $counter++ shows 4 oujtside of my while loop, im sick and tired of why it cant show j ust SHOW "4" inside myloop, ridicilious instead it shows 1234 Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313537 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 this will be my last post, as you do not understand what I am telling you. You are receiving 1234567 as output because each iteration it is outputting a singular number. e.g The first iteration $counter outputs 1, the second iteration $counter outputs 2, etc.. the value of $counter does not equal 1234567 as it appears, all 7 values are simply next to each other. If you want to see for yourself, instead of just echoing $counter, echo $counter . "<br />"; and then see what your results are. The code that you need to find the last iteration is on this thread no less than 3 times. There is no more need to write more code that you will not understand. Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313540 Share on other sites More sharing options...
Monkuar Posted February 2, 2012 Author Share Posted February 2, 2012 this will be my last post, as you do not understand what I am telling you. You are receiving 1234567 as output because each iteration it is outputting a singular number. e.g The first iteration $counter outputs 1, the second iteration $counter outputs 2, etc.. the value of $counter does not equal 1234567 as it appears, all 7 values are simply next to each other. If you want to see for yourself, instead of just echoing $counter, echo $counter . "<br />"; and then see what your results are. The code that you need to find the last iteration is on this thread no less than 3 times. There is no more need to write more code that you will not understand. Your code does not WORK, because it's using get num_rows from my query, which is from all my users data. Look let me explain. I have a Track Topic feature, people click "track Topic" on a topic, it adds it to there Tracker. When they view there tracker, it shows them each topic in EACH forum (Category) so I am trying to display a </dd> after each CATEGORY, hence: if ($counter > $i){ $this->output .= <<< E </table></dd> E; } or whatever kind of code that works, If I use your CODE, it does not because it's gather all my TOPICS and not for each individual category. I need to add the </dd> below every category to make my page look nice. This coderight here, echo's out each Category/forum name if ($last_forum_id != $topic['forum_id']) { $last_forum_id = $topic['forum_id']; $this->output .= <<< E <td colspan=10><dl><dt><a href=?z={$topic['forum_id']}>{$topic['forum_name']}</a></dt><dd> <table class="tuble"> 25 E; } So that works fine, I am puzzled on why I cant use this at the bottom to and this whole thing will be fixed. If I have 4 Category's and 3 topics in each 1, that code shows all 4 categorys with 3 topics in each properly. I just cant use that function again because if I Could iwouldn't have this problem. all I am trying to get is the amount of Category's the person is subscribed to, and if I enter in a $i++ in that code ABOVE, it shows how many, but I cant use it inside my LOOP because like you said it would echo out 1234 instead of just "4" and make my statement not work, this is really making me ache Im about to tear my hair out I feel like a retard Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313541 Share on other sites More sharing options...
kicken Posted February 2, 2012 Share Posted February 2, 2012 Sounds to me like you want to output a </table></td> after each category change. If that is the case I do not see how your use of the $counter variable will help you at all because you still won't know at which count you want to output the closing tags. What you need to do is output them in the same place your outputting the category header, except on the first one. Example: $DB->query("SELECT s.trid, s.member_id, s.topic_id, s.last_sent, s.start_date as track_started, t.*, f.id as forum_id, f.name as forum_name, f.read_perms " ."FROM ibf_tracker s, ibf_topics t, ibf_forums f " ."WHERE s.member_id='".$this->member['id']."' AND t.tid=s.topic_id AND f.id=t.forum_id " ."ORDER BY f.id, t.last_post DESC"); if ( $DB->get_num_rows() ) { $last_forum_id = -1; while( $topic = $DB->fetch_row() ) { if ($last_forum_id != $topic['forum_id']) { if ($last_forum_id != -1){ //close previous category $this->output .= '</table></td>'; } $last_forum_id = $topic['forum_id']; $this->output .= <<< E <td colspan=10><dl><dt><a href=?z={$topic['forum_id']}>{$topic['forum_name']}</a></dt><dd> <table class="tuble"> 25 E; } //[..snip lots of stuff..] } if ($last_forum_id != -1){ //close final category $this->output .= '</table></td>'; } } else { $this->output .= $this->html->subs_none(); } Quote Link to comment https://forums.phpfreaks.com/topic/256220-counter-needs-to-count-the-sum-not-12345/#findComment-1313749 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.