Reputation: 11
How to order randomly data in a database table and insert into other table the results? I mean that i have a list of rows in a table called house.then i need to to fetch those rows and order them randomly and insert into other table called winner.
How can i do this using php?
//***Menaharia and Studio Applicants***//
$aplSTMen=mysql_query('select * from applicant where HouseType="Studio" AND SiteId="1"');
$ApConutTotMenST = mysql_num_rows($aplSTMen);
$querysStudiomenaharia=mysql_query('select * from house where HouseType="Studio" AND SiteId="1"');
$HoTotMenST = mysql_num_rows($querysStudiomenaharia);
//***THE NUMBER OF APPLICANTS ARE EQUAL TO THE NUMBER OF HOUSES***//
if( $HoTotMenST == $ApConutTotMenST) {
while($rw1=mysql_fetch_array($aplSTMen)) {
$ApplicantId= $rw1['ApplicantId'];
$SiteId= $rw1['SiteId'];
$ServiceType= $rw1['ServiceType'];
$HouseType= $rw1['HouseType'];
$PhysicalStatus= $rw1['PhysicalStatus'];
$Gender= $rw1['Gender'];
$MartialStatus=$rw1['MartialStatus'];
$sql = mysql_query("INSERT INTO `condominium`.`winner` (`WinnerId`)VALUES ('$ApplicantId');");
}
$ararr[]=array($aplSTMen);
while($rw1=mysql_fetch_array($querysStudiomenaharia)) {
$ress=$rw1['reserved'];
if( $ress== "no") {
$BlockNo=$rw1['BlockNo'];
$SiteId=$rw1['SiteId'];
$HouseNumber=$rw1['HouseNumber'];
$ararr[]=$HouseNumber;
echo $HouseNumber."<br\>";
/*$sql = mysql_query("INSERT INTO `condominium`.`winner` (`WinnerId`, `SiteId`, `HouseNumber`, `BlockNo`)
VALUES ('$ApplicantId', '$SiteId',$HouseNumber , '$BlockNo');");*/
}
}
}
i have tried the above but it is not working.
Upvotes: 0
Views: 150
Reputation: 3180
No need to copy the data in memory. If you want a psuedo-random order, just add " ORDER BY rand();" to the end of your MySQL queries.
Upvotes: 1