Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I use the following code <div><?php echo $obj->text; ?></div> in while loop.

Is this the best way?

Is there a better way, either to optimize or replace this code?

<?php
    $akhbarkotah1 = $conn->prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
    $akhbarkotah1->execute();
    while($obj = $akhbarkotah1->fetch(PDO::FETCH_OBJ)){

?>
<div><?php echo $obj->text; ?></div>
<?php
    }
?>
share|improve this question

closed as off-topic by Malachi, syb0rg, Simon André Forsberg, rolfl, Jeff Vanzella Mar 3 at 18:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review." – Malachi, syb0rg, rolfl, Jeff Vanzella
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Feel free to fix/rollback my edits if you think I missed something. –  rolfl Mar 3 at 14:54
    
@rolfl thanks , can you add more describe that my problem is using another code replace while($obj = $akhbarkotah1->fetch(PDO::FETCH_OBJ)) –  MinA Mar 3 at 15:29
2  
On CodeReview the requirement is that you post your own code... If you have other code to add, then you should be doing it. –  rolfl Mar 3 at 15:58
    
Does this code work? –  Malachi Mar 3 at 18:36

1 Answer 1

up vote 6 down vote accepted

it looks like you don't really need to exit out of the PHP tag on this one.

<?php
    $akhbarkotah1 = $conn->prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
    $akhbarkotah1->execute();
    while($obj = $akhbarkotah1->fetch(PDO::FETCH_OBJ)){
        echo "<div>" . $obj->text . "</div>";
    }
?>

this simplifies the code and shortens it. I think that I have the Syntax Correct. I am rather new to PHP myself but I am pretty sure that this will work exactly the same.

if you leave your code the other way you should indent the HTML to match what is inside the PHP since it is really inside the PHP (kind of/sort of) like this

<?php
    $akhbarkotah1 = $conn->prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
    $akhbarkotah1->execute();
    while($obj = $akhbarkotah1->fetch(PDO::FETCH_OBJ)){
?>
        <div><?php echo $obj->text; ?></div>
<?php
    }
?>

this just doesn't look right at all.

share|improve this answer
    
thanks , but i have more than a <div> tag html code about 10 line in whaile loop and i dont write here and my main question is for about replace a code for while, for example foreach or dont using loop and ... –  MinA Mar 3 at 15:13
2  
then you probably want to post the full code. because what you have here can be simplified to what I showed you. you can put that same echo line that I used into the while statement. –  Malachi Mar 3 at 15:16
    
my problem is using another code replace while($obj = $akhbarkotah1->fetch(PDO::FETCH_OBJ)) –  MinA Mar 3 at 15:29
    
what do you mean "code replace"? –  Malachi Mar 3 at 16:19
    
i want save array in variable and call with $var[0] –  MinA Mar 3 at 16:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.