Asked  2 Years ago    Answers:  5   Viewed   52 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

2

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

Monday, December 5, 2022
 
blid
 
1

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

Sunday, July 31, 2022
 
4

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).

Sunday, November 6, 2022
3

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;
    }
}
Monday, October 24, 2022
 
cinzel
 
2

From http://www.php.net/manual/en/function.mysql-fetch-array.php

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] ) Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Tuesday, November 8, 2022
 
lycono
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages