[phplib] Re: [Phplib-users] DB class From: Richard Archer (rha <email protected>)
Date: 09/05/01

At 2:17 PM +0200 5/9/01, Guenther Theilen wrote:

>I've got a table with about 100 columns (value either 0 or 1).
>Now I choose exactly one row with $db->query ("SELECT...")
>For each column there is a picto. The name of the picto is
>name_of_column.gif
>I want to show all the pictos where the value in the column is 1.
>Of course one way would be like this:
>$test=$db->f("foo")
>if ($test==1)
> {show foo.gif}
>
>With about 100 columns which even might change in the next time, this
>seems quite complicated to me.
>Is there a more elegant way to get the names of the coloumns where the
>value is 1 out of $db?

Hi Guenther,

I see you have a solution already, but for the record, this is how I
would go about this:

<?php
error_reporting(2047);

/*
  # Database definition including sample data:
  #
  # MySQL dump 6.6
  #
  # Host: localhost Database: j0
  #--------------------------------------------------------
  # Server version 3.23.7-alpha-log

  #
  # Table structure for table 'idtable'
  #
  CREATE TABLE idtable (
    id int(11) DEFAULT '0' NOT NULL,
    foo int(11) DEFAULT '0' NOT NULL,
    PRIMARY KEY (id,foo)
  );

  #
  # Dumping data for table 'idtable'
  #

  INSERT INTO idtable VALUES (1,2);
  INSERT INTO idtable VALUES (2,1);
  INSERT INTO idtable VALUES (3,1);
  INSERT INTO idtable VALUES (3,2);
  INSERT INTO idtable VALUES (3,3);
  INSERT INTO idtable VALUES (3,4);

  #
  # Table structure for table 'footable'
  #
  CREATE TABLE footable (
    foo int(11) DEFAULT '0' NOT NULL,
    gif char(12),
    PRIMARY KEY (foo)
  );

  #
  # Dumping data for table 'footable'
  #

  INSERT INTO footable VALUES (1,'foo1.gif');
  INSERT INTO footable VALUES (2,'foo2.gif');
  INSERT INTO footable VALUES (3,'foo3.gif');

*/

  include "db_mysql.inc";
  $db = new DB_Sql;

  echo "<pre>\n";

  for ($cc = 1; $cc <=3 ; $cc++) {
    $db->query("select idtable.id, idtable.foo, footable.gif " .
               "from idtable, footable " .
               "where idtable.id=$cc and footable.foo=idtable.foo");
    while ($db->next_record()) {
      echo " id = " . $db->Record["id"] . "\n";
      echo "foo = " . $db->Record["foo"] . "\n";
      echo "gif = " . $db->Record["gif"] . "\n";
      echo "\n";
    }
  }

  echo "</pre>\n";

?>

-- 
Abbestellen mit Mail an:   phplib-unsubscribe <email protected>
Kommandoliste mit Mail an: phplib-help <email protected>