Asked  7 Months ago    Answers:  5   Viewed   26 times

i want to know how do we convert the following code to work with foreach

$query_select = "SELECT * FROM shouts ORDER BY id DESC LIMIT 8;"; 

    $result_select = mysql_query($query_select) or die(mysql_error());

    while($row = mysql_fetch_array($result_select)) {
        $ename = stripslashes($row['name']);
        $eemail = stripcslashes($row['email']);
        $epost = stripslashes($row['post']);
        $eid = $row['id'];

        $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70";

        echo ('<img src = "' . $grav_url . '" alt="Gravatar">'.'<br/>');

        echo $eid . '<br/>';

        echo $ename . '<br/>';

        echo $eemail . '<br/>';

        echo $epost . '<br/><br/><br/><br/>';

 Answers

35

You can code like this:

$query_select = "SELECT * FROM shouts ORDER BY id DESC LIMIT 8;";
$result_select = mysql_query($query_select) or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array($result_select))
    $rows[] = $row;
foreach($rows as $row){ 
    $ename = stripslashes($row['name']);
    $eemail = stripcslashes($row['email']);
    $epost = stripslashes($row['post']);
    $eid = $row['id'];

    $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70";

    echo ('<img src = "' . $grav_url . '" alt="Gravatar">'.'<br/>');

    echo $eid . '<br/>';

    echo $ename . '<br/>';

    echo $eemail . '<br/>';

    echo $epost . '<br/><br/><br/><br/>';
}

As you can see, it's still need a loop while to get data from mysql_fetch_array

Wednesday, March 31, 2021
 
leebriggs
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
37

mysql_fetch_array only fetches one row at a time. You can use a while loop to continue fetching rows. The mysql_fetch_array function returns false once the whole result set has been fetched, so that will cause the while loop to terminate as desired.

Also, I removed the foreach loop on $ids. Since there will only be one element in the array it's unnecessary to put the code in a loop.

<?php
$json = json_decode('{
                      "33540116":
                                 {"person":
                                           {"name":"John", "age":"36"}},
                      "33541502":
                                 {"person":                                                                                      
                                           {"name":"Jack", "age":"23"}}
                     }
        ');

$result = mysql_query("SELECT id FROM people");

while ( ( $row = mysql_fetch_array($result) ) ) {
    $id = $row['id'];

    echo $json->$id->person->{'name'}. '<br />';
    echo $json->$id->person->{'age'}. '<br />';
}

?>
Saturday, May 29, 2021
 
tika
answered 5 Months ago
52

As I have long time not touching Unity... but I do believe you mess up in the calculation.

First of all, Vector3.MoveTowards(currentPos, toPosition, time) is talking about

Walking from currentPos to toPosition with each frame moving certain distance time.

Thus using time as name is confusing, but fine lets keep it.

However, you will notice in the statement, time is something move each frame. But Vector3.Distance(currentPos, toPosition) / duration is velocity (m/s), not (m/frame). To make it (m/frame), simply times Time.deltatime which is (s/frame).

Secondly,

When in coroutine, which means the function is iterated each frame. In the line float time = Vector3.Distance(currentPos, toPosition) / duration * Time.deltatime;, what you will notice is that the time keeps on decreasing when going next frame as distance become smaller and smaller.

To be more concrete, we can do some math. Typically this should be done with calculus, but lets simplify it by considering just 2 points. When object postion d = 0, and object postion d ~= 9.9, assume endpoint at 10.

At point 1, the object has time = (10-0)/duration, full speed. At point 2, the object has time = (10-9.9)/duration, 1/10 of full speed.

Unless you want it to move slower every frame, you cannot hold the value of duration unchanged. As after each frame, you want the velocity to be kept, duration should thus decreases with distance.

To make that physics work, minus the duration for the time passed.

So the final solution is

float time = Vector3.Distance(currentPos, toPosition) / (duration-counter) * Time.deltaTime;

Here is the complete function:

IEnumerator MoveTowards(Transform objectToMove, Vector3 toPosition, float duration)
{
    float counter = 0;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        Vector3 currentPos = objectToMove.position;

        float time = Vector3.Distance(currentPos, toPosition) / (duration - counter) * Time.deltaTime;

        objectToMove.position = Vector3.MoveTowards(currentPos, toPosition, time);

        Debug.Log(counter + " / " + duration);
        yield return null;
    }
}
Tuesday, August 24, 2021
 
billz
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :