Joe Austin
Joe Austin

Reputation: 557

Code repetition (PHP)

I'm trying not to repeat code. Is there any way to get this working so I don't have to write out the code three times for the three people? Forgive my ignorance: I'm a beginner.

$c1 = "Rufus T Firefly";
$c2 = "Chicolini";
$c3 = "Pinky";

for ($i=1; $i<=3; $i++){
  $result = mysqli_query($dbc, ' SELECT * FROM voters WHERE choice = "'. $c($i).'" ');
  //create table...
}

Upvotes: 1

Views: 140

Answers (1)

John Conde
John Conde

Reputation: 219814

Use IN()

$sql = "SELECT * FROM voters WHERE choice IN('Rufus T Firefly', 'Chicolini', 'Pinky')"
$result = mysqli_query($dbc, $sql);

Now no loops or arrays needed (to generate your query).

Upvotes: 4

Related Questions