Mc Abhel Valerio
Mc Abhel Valerio

Reputation: 41

Why is it that the ID will appear instead of name?

I have a plsvote.php that will handle your choosen candidates and then there is a preview.php that will handle who were your candidates. The problem is instead the name of the candidates it will appear the candidates id like for example in the picture Here's the picture for example:

enter image description here

Here's my code(plsvote.php)

<?php 
include('connection/connect.php'); 
$YearNow=Date('Y'); 
$dsds=$rowasa['posid']; 
$results = $db->prepare("SELECT * FROM candidates,student,school_year,partylist where student.idno = candidates.idno AND school_year.syearid = candidates.syearid AND posid =:a AND candidates.partyid = partylist.partyid AND school_year.from_year like $YearNow"); 

$results->bindParam(':a', $dsds); 
$results->execute(); 
for($i=0; $rows = $results->fetch(); $i++){ 
?> 
<option style="padding: 35px 50px 35px 80px; background:url('admin/savephp/images/<?p... echo $rows['image']; ?>') no-repeat scroll 5px 7px / 70px auto rgba(0, 0, 0, 0);" 

value="<?php echo $rows['candid'] ?>"><?php echo $rows['lastname'] ?>, <?php echo $rows['firstname'] ?> 
- <?php 
echo $rows['party_name']?></option> 

And (Preview.php)

<?php 
$resultasa = $db->prepare("SELECT * FROM candposition"); 
$resultasa->execute(); 
for($i=0; $rowasa = $resultasa->fetch(); $i++){ 
$exrxrxrx=$rowasa['pos_name']; 

if ($exrxrxrx!='Error') { 
?> 
<h2 class="fs-title"><?php echo $exrxrxrx ?></h2> 
<input type="hidden" value="<?php echo $_POST[$exrxrxrx] ?>" name="votes[]" /><?php echo $_POST[$exrxrxrx] ?><br>

// this part will appear the candidates ID I think because of the value of the plsvote.php and it will be based on it but I want that the name of the candidates will appear not there id. Hope you help me thanks

Upvotes: 3

Views: 73

Answers (2)

Avenar
Avenar

Reputation: 341

Assuming you're posting the data from plsvote.php to preview.php, your options' value is set as such:

// from plsvote.php
value="<?php echo $rows['candid'] ?>"

Assuming this is the "candidate id" picked from your database in your query, this is the only value sent along to your preview.php, through post, where you call:

// from preview.php
value="<?php echo $_POST[$exrxrxrx] ?>"

To access the name, you'll either have to pass it along in the value:

// for plsvote.php
value="<?php echo $rows['candid'] . "#" . $rows['lastname'] . " " . $rows['firstname'] ?>"

and then extract it again:

// for preview.php
<input type="hidden" value="<?php echo explode("#", $_POST[$exrxrxrx])[0] ?>" name="votes[]" /><?php echo explode("#", $_POST[$exrxrxrx])[1] ?><br>

Or retrieve the name based on the ID for each candidate. (my recommendation based on the example, but if this is a huge dataset, it might not be the ideal solution):

// for preview.php
$name_result = $db->prepare("SELECT s.firstname,s.lastname FROM candidates c JOIN students s ON c.idno = s.idno WHERE c.candid = :a LIMIT 1");
$name_result->bindParam(':a', $_POST[$exrxrxrx]); 
$name_result->execute();
?>
<input type="hidden" value="<?php echo $name_result['firstname'] . " " . $name_result['lastname'] ?>" name="votes[]" /><?php echo $name_result['firstname'] . " " . $name_result['lastname'] ?><br>

This makes some assumtions about how your database looks, which i don't know, but if you've made it as far as you have, i'm sure you can adjust it to fit, if this is your chosen solution.

Upvotes: 2

rain
rain

Reputation: 243

For this part

 <option style="padding: 35px 50px 35px 80px; background:url('admin/savephp/images/<?p... echo $rows['image']; ?>') no-repeat scroll 5px 7px / 70px auto rgba(0, 0, 0, 0);" 

value="<?php echo $rows['candid'] ?>"><?php echo $rows['lastname'] ?>, <?php echo $rows['firstname'] ?> 
- <?php 
echo $rows['party_name']?></option> 

value="<?php echo $rows['candid']--->This is the value you'll get in PHP.If you want to get the name,try to change it to name like

value="<?php echo $rows['candName']"

Upvotes: 2

Related Questions