I could not find an answer to this question or I completely missed the answer, what I am doing is looping from a database, just a number value, this variable $competition['competition_number_of_tickets'] could be for example 200.
<?php
$counter = 0;
for ($i = 1; $competition['competition_number_of_tickets'] >= $i; $i ) {
if ($i % 10 === 0) {
echo "<button type=\"button\" class=\"btn btn-primary\">{$i}</button>";
} else {
echo "<hr />";
}
}
?>
The way my code is now, each button lists 1 after each other, what I'm trying to do is have 10 buttons on each row to make it look nicer, I know that my code is not working as expected could anyone explain what I'm doing wrong? I'm not great with maths.
CodePudding user response:
Your loop should compare $i
to the total, as in
for ($i = 1; $i <= $competition['competition_number_of_tickets']; $i )
Then, your modulus will fire every time the counter gets to a multiple of 10, but the way you have it now (which is reversed from how you want it) it will show a button only when the counter gets to a multiple of 10. Even if you had that right, it would still ignore ignore the 10th button. Your if statement should look more like
echo "<button type=\"button\" class=\"btn btn-primary\">{$i}</button>";
if ($i % 10 === 0) echo "<hr />";
CodePudding user response:
Alternative answer
You can use array_chunk
and range
:
$data = array_chunk(range(1, $competition['competition_number_of_tickets']), 10, true);
foreach ($data as $items) {
foreach ($items as $item) {
echo "<button type=\"button\" class=\"btn btn-primary\">" . $item . "</button>";
}
echo '<hr>';
}
Output :
<button type="button" >1</button>
<button type="button" >2</button>
<button type="button" >3</button>
<button type="button" >4</button>
<button type="button" >5</button>
<button type="button" >6</button>
<button type="button" >7</button>
<button type="button" >8</button>
<button type="button" >9</button>
<button type="button" >10</button>
<hr />
<button type="button" >11</button>
<button type="button" >12</button>
<button type="button" >13</button>
<button type="button" >14</button>
<button type="button" >15</button>
<button type="button" >16</button>
<button type="button" >17</button>
<button type="button" >18</button>
<button type="button" >19</button>
<button type="button" >20</button>
<hr />
<button type="button" >21</button>
<button type="button" >22</button>
<button type="button" >23</button>
<button type="button" >24</button>
<button type="button" >25</button>
<button type="button" >26</button>
<button type="button" >27</button>
<button type="button" >28</button>
<button type="button" >29</button>
<button type="button" >30</button>
<hr />
<button type="button" >31</button>
<button type="button" >32</button>
<button type="button" >33</button>
<button type="button" >34</button>
<button type="button" >35</button>
<button type="button" >36</button>
<button type="button" >37</button>
<button type="button" >38</button>
<button type="button" >39</button>
<button type="button" >40</button>
<hr />
<button type="button" >41</button>
<button type="button" >42</button>
<button type="button" >43</button>
<button type="button" >44</button>
<button type="button" >45</button>
<button type="button" >46</button>
<button type="button" >47</button>
<button type="button" >48</button>
<button type="button" >49</button>
<button type="button" >50</button>
<hr />
<button type="button" >51</button>
<button type="button" >52</button>
<button type="button" >53</button>
<button type="button" >54</button>
<button type="button" >55</button>
<button type="button" >56</button>
<button type="button" >57</button>
<button type="button" >58</button>
<button type="button" >59</button>
<button type="button" >60</button>
<hr />
<button type="button" >61</button>
<button type="button" >62</button>
<button type="button" >63</button>
<button type="button" >64</button>
<button type="button" >65</button>
<button type="button" >66</button>
<button type="button" >67</button>
<button type="button" >68</button>
<button type="button" >69</button>
<button type="button" >70</button>
<hr />
<button type="button" >71</button>
<button type="button" >72</button>
<button type="button" >73</button>
<button type="button" >74</button>
<button type="button" >75</button>
<button type="button" >76</button>
<button type="button" >77</button>
<button type="button" >78</button>
<button type="button" >79</button>
<button type="button" >80</button>
<hr />
<button type="button" >81</button>
<button type="button" >82</button>
<button type="button" >83</button>
<button type="button" >84</button>
<button type="button" >85</button>
<button type="button" >86</button>
<button type="button" >87</button>
<button type="button" >88</button>
<button type="button" >89</button>
<button type="button" >90</button>
<hr />
<button type="button" >91</button>
<button type="button" >92</button>
<button type="button" >93</button>
<button type="button" >94</button>
<button type="button" >95</button>
<button type="button" >96</button>
<button type="button" >97</button>
<button type="button" >98</button>
<button type="button" >99</button>
<button type="button" >100</button>
<hr />
<button type="button" >101</button>
<button type="button" >102</button>
<button type="button" >103</button>
<button type="button" >104</button>
<button type="button" >105</button>
<button type="button" >106</button>
<button type="button" >107</button>
<button type="button" >108</button>
<button type="button" >109</button>
<button type="button" >110</button>
<hr />
<button type="button" >111</button>
<button type="button" >112</button>
<button type="button" >113</button>
<button type="button" >114</button>
<button type="button" >115</button>
<button type="button" >116</button>
<button type="button" >117</button>
<button type="button" >118</button>
<button type="button" >119</button>
<button type="button" >120</button>
<hr />
<button type="button" >121</button>
<button type="button" >122</button>
<button type="button" >123</button>
<button type="button" >124</button>
<button type="button" >125</button>
<button type="button" >126</button>
<button type="button" >127</button>
<button type="button" >128</button>
<button type="button" >129</button>
<button type="button" >130</button>
<hr />
<button type="button" >131</button>
<button type="button" >132</button>
<button type="button" >133</button>
<button type="button" >134</button>
<button type="button" >135</button>
<button type="button" >136</button>
<button type="button" >137</button>
<button type="button" >138</button>
<button type="button" >139</button>
<button type="button" >140</button>
<hr />
<button type="button" >141</button>
<button type="button" >142</button>
<button type="button" >143</button>
<button type="button" >144</button>
<button type="button" >145</button>
<button type="button" >146</button>
<button type="button" >147</button>
<button type="button" >148</button>
<button type="button" >149</button>
<button type="button" >150</button>
<hr />
<button type="button" >151</button>
<button type="button" >152</button>
<button type="button" >153</button>
<button type="button" >154</button>
<button type="button" >155</button>
<button type="button" >156</button>
<button type="button" >157</button>
<button type="button" >158</button>
<button type="button" >159</button>
<button type="button" >160</button>
<hr />
<button type="button" >161</button>
<button type="button" >162</button>
<button type="button" >163</button>
<button type="button" >164</button>
<button type="button" >165</button>
<button type="button" >166</button>
<button type="button" >167</button>
<button type="button" >168</button>
<button type="button" >169</button>
<button type="button" >170</button>
<hr />
<button type="button" >171</button>
<button type="button" >172</button>
<button type="button" >173</button>
<button type="button" >174</button>
<button type="button" >175</button>
<button type="button" >176</button>
<button type="button" >177</button>
<button type="button" >178</button>
<button type="button" >179</button>
<button type="button" >180</button>
<hr />
<button type="button" >181</button>
<button type="button" >182</button>
<button type="button" >183</button>
<button type="button" >184</button>
<button type="button" >185</button>
<button type="button" >186</button>
<button type="button" >187</button>
<button type="button" >188</button>
<button type="button" >189</button>
<button type="button" >190</button>
<hr />
<button type="button" >191</button>
<button type="button" >192</button>
<button type="button" >193</button>
<button type="button" >194</button>
<button type="button" >195</button>
<button type="button" >196</button>
<button type="button" >197</button>
<button type="button" >198</button>
<button type="button" >199</button>
<button type="button" >200</button>
<hr />