Jump to content

is there an easier way to do this ????


imarockstar

Recommended Posts

Ok ... I am not sure if there is an easier way to grab the data fields from the database ... As of know I am typing them all out and with TABLES with over like 50 fields ..its a huge PAIN IN THE ASS !!!

 

 

first I select the DB and what I want to see


//from previous page
$resumeid = $_GET['resumeid'];


//select the table
$result = mysql_query("select * from resume where resumeid='$resumeid'");

while($r=mysql_fetch_array($result))
{	
   
  
   // how can I avoid doing this .. an array maybe ?

   $date =$r["date"];
   $resumeid =$r["resumeid"];
   $jobtitle =$r["jobtitle"];
   $title =$r["title"];
   $fname =$r["fname"];
   $lname =$r["lname"];
   $address =$r["address"];
   $city =$r["city"];
   $state =$r["state"];
   $zipcode =$r["zipcode"];
   $phone1 =$r["phone1"];
   $phone2 =$r["phone2"];
   $salary =$r["salary"];
   $rate =$r["rate"];
   $eligibility =$r["eligibility"];
   $travel =$r["travel"];
   $startdate =$r["startdate"];
   $notice =$r["notice"];
   $heard =$r["heard"];
   $resume =$r["resume"];
   $comments =$r["comments"];
?>


 

 

after i type all that, i need to put the DB data into my html ...

 

<h2 class=><?=$title ?> <?=$fname ?> <?=$lname ?></h2>

<?=$resume ?>

 

 

<?php } ?>

 

 

 

 

 

how can I avoid typing out all the fields ???

 

 

Link to comment
https://forums.phpfreaks.com/topic/120215-is-there-an-easier-way-to-do-this/
Share on other sites

you could always use the array variables directly, eg use $r['blah'] instead of $blah.

 

If u NEED the variables, then yes, fortunately there is a way to assign the variables in a few simple lines:

 

<?php

//from previous page
$resumeid = $_GET['resumeid'];

//select the table
$result = mysql_query("select * from resume where resumeid='$resumeid'");

while($r=mysql_fetch_array($result))
{	
   $Inverted = array_flip($r); // Switch the array "ID"'s with the Array "Values" and assign the result to $Inverted
   $c = count($r);               // Count how many variables need to be assigned
   
   For($i=0;$i<$c;$i++){      // Loop Each variable and assign
      $$Inverted[$i] = $r[$Inverted[$i]]; // This uses $$variablename, you assign a variable with the name of the value of $variablename.
   }
}
?>

 

This is infact harsh code, slow. Sticking with the original arrays is much faster and cleaner.

This is how i personally would have this code:

 

<?php

//from previous page
$resumeid = $_GET['resumeid'];

//select the table
$result = mysql_query("select * from resume where resumeid='$resumeid'");

while($r=mysql_fetch_array($result))
{	
   $Results[] = $r;
}
?>

 

$Results would become a Multi-Dimensional array. Eg:

 

$Results[1] = the first row.

$Results[2]['state'] = The State from the Second Returned Row.

---

 

Hope this helps, and happy coding :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.