Page 1 of 1

Selecting a particular record

Posted: Sat Jan 04, 2003 3:37 am
by Beady
Hi,

I have a MySQL database with two tables. I need to be able to show a list of records from one table and one field from the other table like this
Table1Title1 Table1SubHeading1 Table2PageName Table1Desc1
Table1Title2 Table1SubHeading2 Table2OtherPageName Table1Desc2

Can someone point me in the right direction please

Regards

Bernard Davis

Posted: Sat Jan 04, 2003 6:23 am
by Joan Garnet
Well,
there are other ways...
but I think this is faster -->


Code: Select all

<?php 
$server = "localhost" ;
$login = "your_login" ;
$pass = "your_pass";
$table1 = "table1_name";
$table2 = "table2_name";
$data_base = "my_database_name";
$conection = mysql_connect($server,$login,$pass);     
mysql_select_db($data_base,$conection);
////////////////////////////////////////////////////////
$sql_one = "select * from ".$table1." order by id desc";
$sql_two = "select * from ".$table2." order by id desc";
////////////////////////////////////////////////////////
$result_one=mysql_query($sql_one , $conection);
$result_two=mysql_query($sql_two , $conection);
////////////////////////////////////////////////////////
$i=0;
while ($row = mysql_fetch_array ($result_two)) {
   for ($j=0; $j<mysql_num_rows($result_two); $j++){
      $my_array[$j][$i] = $row[$j];
   }
   $i++;
}
print_r ($my_array);//you'll see the shape of this array, just comment this two lines when you see it clear
echo"<p>";
////////////////////////////////////////////////////////////////
/// This is the output. You can define it as you want ////
///////////////////////////////////////////////////////////////
while ($row = mysql_fetch_array ($result_one)) {
   echo $row[0]." - ".$row[1]." - ".$my_array[0][1]." - ".$row[2]." - ".$row[3]." - "."<br>";
}
?>


I hope It helped
bye!

Almost but not quite

Posted: Sun Jan 05, 2003 3:08 am
by Beady
Hi,

Many thanks for your response, it almost does the trick. I can see how you are using an array to build the list of entries, but I still can't get it to work.

Perhaps if I ask a slightly different question -

How do I simply build an array which contains one field (say 'pageName' or field number 1) from every record in a table?

I know how to access the array once it is built, but I can't see how it is built in the first place.

Regards

Bernard Davis

Posted: Sun Jan 05, 2003 10:05 am
by Joan Garnet
Maybe...

Code: Select all

while ($row = mysql_fetch_array ($result)) { 
   echo $row["pageName"];
}


:?:

Posted: Fri Feb 14, 2003 12:06 pm
by Joan Garnet
ok,
I think this example will help you:

Code: Select all

<?php 
$my_string = "    job , setup , office , supplies ";
$my_array = explode(",", $my_string);
for($i=0;$i<sizeof($my_array);$i++){
   $no_white_spaces = trim($my_array[$i]);
   echo $no_white_spaces."<br>";   
}
?>


bye!