function to simplify data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • windandwaves

    function to simplify data

    Hi Folk

    I seem to be writing the following a lot (this is just one of many
    examples):

    $sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
    $query = mysql_query($sq l);
    while($row = mysql_fetch_row ($query)) {
    $ei = $row[0];
    $en = $row[1];
    $content .= '<li><a href="test.php? i='.$data("ID") .......
    }

    I want to write a function like this

    function getdata($sql) {
    $query = mysql_query($sq l);
    while($row = mysql_fetch_row ($query)) {
    $v[$i][{name of first column in select query (e.g. ID)}] = $row[0];
    $v[$i][{name of first column in select query (e.g. NAM)}] = $row[1];
    $i++;
    }

    Then, in my code I can write something like

    $sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
    $data = getdata($sql)
    while(loop through array - i have no idea how you do this - but can look it
    up) {
    $content .= '<li><a href="test.php? i='.$data("ID") .......
    }

    What is the most efficient way to write such a function???

    Any help appreciated.

    - Nicolaas


  • Janwillem Borleffs

    #2
    Re: function to simplify data

    windandwaves wrote:[color=blue]
    > What is the most efficient way to write such a function???
    >[/color]

    function getdata($sql) {
    $result = @mysql_query($s ql) or die("Error: " . mysql_error());
    $ret = array();

    while($row = mysql_fetch_ass oc($result)) {
    $ret[] = $row;
    }
    mysql_free_resu lt($result);
    return $ret;
    }


    $sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
    $data = getdata($sql)

    foreach ($data as $d) {
    $content .= '<li><a href="test.php? i='.$d["ID"].......
    }


    JW



    Comment

    • windandwaves

      #3
      Re: function to simplify data

      Janwillem Borleffs wrote:[color=blue]
      > windandwaves wrote:[color=green]
      >> What is the most efficient way to write such a function???
      >>[/color]
      >
      > function getdata($sql) {
      > $result = @mysql_query($s ql) or die("Error: " . mysql_error());
      > $ret = array();
      >
      > while($row = mysql_fetch_ass oc($result)) {
      > $ret[] = $row;
      > }
      > mysql_free_resu lt($result);
      > return $ret;
      > }
      >
      >
      > $sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
      > $data = getdata($sql)
      >
      > foreach ($data as $d) {
      > $content .= '<li><a href="test.php? i='.$d["ID"].......
      > }[/color]


      Thank you SO MUCH.... Awesome


      Comment

      • windandwaves

        #4
        Re: function to simplify data

        windandwaves wrote:[color=blue]
        > Janwillem Borleffs wrote:[color=green]
        >> windandwaves wrote:[color=darkred]
        >>> What is the most efficient way to write such a function???
        >>>[/color]
        >>
        >> function getdata($sql) {
        >> $result = @mysql_query($s ql) or die("Error: " . mysql_error());
        >> $ret = array();
        >>
        >> while($row = mysql_fetch_ass oc($result)) {
        >> $ret[] = $row;
        >> }
        >> mysql_free_resu lt($result);
        >> return $ret;
        >> }
        >>
        >>
        >> $sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
        >> $data = getdata($sql)
        >>
        >> foreach ($data as $d) {
        >> $content .= '<li><a href="test.php? i='.$d["ID"].......[/color][/color]


        Does it slow things down to use proper names rather than numbers, i.e.

        $d["NAM"] rather than $d[0]

        Thank you again

        - Nicolaas


        Comment

        • Janwillem Borleffs

          #5
          Re: function to simplify data

          windandwaves wrote:[color=blue]
          > Does it slow things down to use proper names rather than numbers, i.e.
          >
          > $d["NAM"] rather than $d[0]
          >[/color]

          No, but using named keys is more verbose then using array indexes. When
          referencing a key "NAM" then you can be sure you get the value of the key
          "NAM"; with an index this doesn't have to be the case.


          JW



          Comment

          • R. Rajesh Jeba Anbiah

            #6
            Re: function to simplify data

            Janwillem Borleffs wrote:[color=blue]
            > windandwaves wrote:[color=green]
            > > Does it slow things down to use proper names rather than numbers, i.e.
            > >
            > > $d["NAM"] rather than $d[0]
            > >[/color]
            >
            > No, but using named keys is more verbose then using array indexes. When
            > referencing a key "NAM" then you can be sure you get the value of the key
            > "NAM"; with an index this doesn't have to be the case.[/color]

            I'm also a big fan of hash arrays; I use them often for readability.
            But, AFAIK, hash array lookup takes linear search and would be slower
            in terms of speed. (But, I'm not sure about the implementation in PHP)

            --
            <?php echo 'Just another PHP saint'; ?>
            Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

            Comment

            Working...