Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using $i string for limit in foreach loop.
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine'){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
}
if($menu_style == 'normal'){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}
I can't want to use LIMIT in SQL query.
And tell me when i use if($i == 5){ break; } then code display just 4
menu name
Tell me how is display menu name as my required.
Something like that?
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine' && $i <= 5){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
} else if($menu_style == 'normal' && $i <= 4){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
There is a block of conditions.Help to shorten and improve this part of the code
$stock = $row['KOLVO_T'];
if ($row['CENA'] < 1000 && $row['KOLVO_T'] == 1) {
$stock = 0;
}
if ($row['CENA'] >= 1000 && $row['KOLVO_T'] == 1) {
$stock = $row['KOLVO_T'];
}
if ($row['KOLVO_T'] >= 2) {
$stock = $row['KOLVO_T'];
}
return $stock;
I don't really see the point, but here's the simplest I could do:
$stock = $row['KOLVO_T'];
if ($row['CENA'] < 1000 && $stock == 1) {
$stock = 0;
}
return $stock;
The last two conditional blocks are useless since they repeat an operation you already did before the first conditional block, and their condition can't be verified if the first is.
On the page I'm creating there is graphic representation of pigeon-holes with five compartments for new comments. If there is new unread comment it should show up as graphic in one random compartment. But it won't happen if all of the 5 are
already occupied. So if someone writes new comment I like the code to check for if there is already five of them taken, and if not, than to randomly occupied one of the remaining ones. So "new_c" stands for number of
unread (new) comments, and c1-c5 stands for compartments. Value 0 means empty compartment for each "c". I was trying to make code that would first separate new_c from rest of the array after knowing the number is smaller than 5,
so I'm only working with 5 compartments. And than to determine which one is/are empty. And then randomly choose one empty to occupy. It's not really working as it is I probably am not using that array_keys properly as c2 is changed to some other value than 0 but still is being echoed, there is probably also a better/more efficient way to achieve that. I will really appreciate some input.
<?php
$tucQuery = "SELECT new_c,c1,c2,c3,c4,c5 FROM users WHERE id = '{$author_id}' LIMIT 1";
$result_tuc = mysqli_query($connection, $tucQuery);
$tucArray = mysqli_fetch_assoc($result_tuc);
mysqli_free_result($result_tuc);
$new_c = $tucArray[0];
if($new_c < 5){
$new_array = array_keys((array_slice($tucArray,1)), 0);
$rand_zero = array_rand($new_array, 1);
echo $rand_zero + 1;
}
?>
Bellow code works but it doesn't seem to be efficient and there is probably a better way.
if($new_c < 5){
$empties = array();
if($tucArray['c1'] == 0){
$empties[] = 1;
}
if($tucArray['c2'] == 0){
$empties[] = 2;
}
if($tucArray['c3'] == 0){
$empties[] = 3;
}
if($tucArray['c4'] == 0){
$empties[] = 4;
}
if($tucArray['c5'] == 0){
$empties[] = 5;
}
print_r($empties);
$rand_zero = array_rand((array_flip($empties)), 1);
echo $rand_zero;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i still cannot get this review system to work properly.
i know the average rating is 3.57 but as soon as i add the if() all i am getting is an output of 1?
// collect 5 lastest reviews for viewing
$sql = "SELECT AVG(rating) FROM reviews WHERE review_id = '$id'";
$query = mysqli_query($conn, $sql);
$AvgReview = mysqli_fetch_row($query);
// Here we have the toatal row count
$AvgReviews = $AvgReview[0];
if($AvgReviews = 1){
echo '1star';
}
//next else is new
else if($AvgReviews >= 1){
echo '2star';
}
//next else is new
else if($AvgReviews >= 2){
echo '3star';
}
//next else is new
else if($AvgReviews >= 3){
echo '4star';
}
//next else is new
else if($AvgReviews >= 4){
echo '5star';
}
You are getting '1star' because you are assigning a value of 1 to $AvgReviews.
Change your code to compare the value.
if ($AvgReviews == 1) {
Additionally, you need to reverse your code or you will end up getting 1 or 2 stars, only.
I would recommend to change it like that:
if ($AvgReviews > 4) {
echo '5star';
}
else if($AvgReviews > 3) {
echo '4star';
}
else if($AvgReviews > 2) {
echo '3star';
}
else if($AvgReviews > 1) {
echo '2star';
}
else {
echo '1star';
}
It's because you're assigning 1 to $AvgReviews. The assignment operator (=) takes the value on the right-hand side, assigns it to the name on the left-hand side, and then returns the value that was assigned. In this case, that's always 1, which PHP interprets as true.
Use the comparison operator instead:
if($AvgReviews == 1){
echo '1star';
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I work on a page(.php) that show some information from databases(like article name,article author).I also have a pagination script that work good(the ideea of pagination script is when I press a link the url of the site goes to something like ?page=2)Now I try to numbering the records that I show form databases.But how can I do that?
Pagination script:
$per_pages = 6;
$nrofarticles_query = mysql_query("SELECT COUNT('id') FROM tbl_articles");
$nrdearticole = mysql_result($nrofarticles_query, 0);
$totalpages = ceil($nrdearticole/$per_pages);
$currentpagedisplay = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$firstpair = ($currentpagedisplay - 1) * $per_pages;
I echo record from database like so:
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair, $per_pages");
while($article = mysql_fetch_array(article_set)){
echo $article['article_name'];
}
So I show 6 records from database on page change show the next 6 records or if is less than 6 show how many remained .How can I numbering the records like this:
?page=1
1 article_name1
2 article_name2
3 article_name3
....
6 article_name6
?page=2
keep going
7 article_name7
8 article_name8
.....
EDIT
I was thinking to something like
<?php
$nrofarticles = 0;
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair,$per_pages");
confirm($articles_set);
while($article = mysql_fetch_array($articles_set)){
$nrofarticles ++;
}
echo "<ul id=\"id\">";
for($i = $firstpair+1; $i<=$nrofarticles * $currentpagedisplay; $i++){
echo '<li class="nr_id">' .$i. '<sub>#</sub></li>';
}
echo "</ul>";
?>
It's works but only on first 2 page of 3 because I have 16 articles in total - so on last page $firstpair + 1 will be 13 and $nrofarticles * $currentpagedisplay will be 12(4(articles) * 3(page=3)).
Go use a framework. You'll get pagination (and all sorts of extras) for free.
I echo results in DESC order from MySQL table with while loop. I've pagination system implemented already and its size is 9 records per page. The problem is, that if I do:
// ECHO CSS BREAK
if($row['id'] % 3 == 0){
echo '<li class="clear"></li>';
}
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
echo '<li>...echo code...</li>';
// problem = implement that echo css break in ASC order
}
Use a loop variable, e.g.
$i = 0;
Then instead of
if ($row['id'] % 3 == 0) {
do
if (++$i % 3 === 0) {
This makes sure it always happens are the third [sixth, ninth, ...] time.
You may want to get arbitrary rows from the database at another point in time, or shuffle the results -- relying on row IDs is not a good idea.
Is this what you are looking to implement?
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
if($row['id'] % 3 == 0){
echo '<li>...echo code...</li>';
echo '<li class="clear"></li>';
} else {
echo '<li>...echo code...</li>';
}
}