br3nn4n Posted January 1, 2009 Share Posted January 1, 2009 Just making some changes to my Ajax poll, doing some stuff with GD to create bars. Here's where I'm stuck: I want to echo out an asterisk (*) the number of times a choice has votes. From there I can just replace each one with a small image or whip up something using GD to create a bar to show the results. I can't figure out how I would have it output a character a certain number of times though. I'm thinking possibly do while($r = mysql_fetch_array($q) { for ($i=0,$i < mysql_num_rows($q),$i++) { echo "*"; }} Would this work? Or else how should it be done? Link to comment https://forums.phpfreaks.com/topic/139087-echo-the-number-of-rows-a-query-returns/ Share on other sites More sharing options...
Absorbator Posted January 1, 2009 Share Posted January 1, 2009 Don't use $q in mysql_num_rows as it would cause always the same results. Instead use $r["votes"], assuming that you have a field named votes which stores number of votes... This should works Link to comment https://forums.phpfreaks.com/topic/139087-echo-the-number-of-rows-a-query-returns/#findComment-727515 Share on other sites More sharing options...
br3nn4n Posted January 2, 2009 Author Share Posted January 2, 2009 Yeah, haha..of course...I typed that post at something like 5am yesterday from my phone Thanks! Link to comment https://forums.phpfreaks.com/topic/139087-echo-the-number-of-rows-a-query-returns/#findComment-727748 Share on other sites More sharing options...
kenrbnsn Posted January 2, 2009 Share Posted January 2, 2009 Here are two ways of creating a string containing a specified number of the same character: <?php $votes = range(10,50); shuffle($votes); $rnd = array_slice($votes,0,10); foreach ($rnd as $num) echo $num . ': '. str_pad('',$num,'*') . "\n"; echo "------------------------\n"; foreach ($rnd as $num) echo $num . ': ' . implode('',array_fill(0,$num,'*')) . "\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/139087-echo-the-number-of-rows-a-query-returns/#findComment-727766 Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 I think str_pad() is faster than array_fill() followed by implode(), by the way. Link to comment https://forums.phpfreaks.com/topic/139087-echo-the-number-of-rows-a-query-returns/#findComment-727773 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.