0

I´m new to PHP and SQL and it´s the first time I post here, hope I do it right =)

I need to find out the numeric index from an certain field in my database. Thing is I´m not done with the database structure yet, so for this specific field I´d like to have it automated so I won't have to change the code every time I add or remove columns.

I also need it to have BOTH index types so I can call the columns by name in part of the code and by index for automated parts of it.

I´ve managed to achieve the result I wanted trough an very ugly code, because all the array_search and array_keys I´ve tried didn´t work out for some reason.

Here is the snippet of my working but ugly code. Do you have an more elegant solution?

Thanks in advance for your answers, and for all I´ve learned so far from reading other peoples questions!

$dadosRes = mysqli_query($con, "SELECT * FROM Alunos WHERE Id=1");
$student = mysqli_fetch_array($dadosRes) or die(mysql_error());

$c = 0; // This block is to find out where the column "cont1" is and assign it to $cont1
$d=array_keys($student);
$cont1=0;

foreach ($student as $a=>$b){              
if ($a==='cont1') {
$cont1=$d[$c-1];
}

$c++;
}

for ($i=$cont1; $i<$cont1+12; $i+=3) { // Displays the students contacts, each in up to 3 rows
if ($student[$i]) {
echo "<p><ul><li>".$student[$i]."</li>";
if ($student[$i+1]) echo "<li>".$students[$i+1]."</li>";
if ($student[$i+2]) echo "<li>".$students[$i+2]."</li>";
echo "</ul></p>";
}
}
Tomaz Fernandes
  • 2,429
  • 2
  • 14
  • 18
  • Are you looking for an `AUTO_INCREMENT` column? – Get Off My Lawn Jun 01 '14 at 23:01
  • I think it is easier if explain what $student contains and what $dadosRes. For me it sounds like you are trying to solve the wrong problem – WizKid Jun 01 '14 at 23:02
  • `$cont1=$d[$c-1];` gives the name of the column preceding `cont1`. – bloodyKnuckles Jun 01 '14 at 23:12
  • Edited the question to include $dadosRes, it´s the resources from the database. What I´m trying to achieve is a better way to come up with $cont1, one that does´t have to go trough all the columns. Going to specify that on the question too, thanks! – Tomaz Fernandes Jun 01 '14 at 23:13
  • 1
    don't use `mysql_fetch_array`, but instead `mysql_fetch_assoc` which gives you the columns *by name*. – didierc Jun 01 '14 at 23:15
  • Thing is I need the number index to make the rest of the code work, as in echo "
  • ".$students[$i+1]."
  • "; – Tomaz Fernandes Jun 01 '14 at 23:23
  • What are you wanting to do with the column index. I don't see how you're using that in the last loop. – bloodyKnuckles Jun 01 '14 at 23:29
  • I have 4 contact fields, each of which containing 3 fields of information, all of which can have data or not. They are as 12 columns in the database. The idea is to loop through it so that if the first field has data, it looks the subsequent two for data too, and then the second field, and so on until all data is displayed. I also have other columns before regarding other data from the students, and that number can change, that´s the need for an automated index finder... – Tomaz Fernandes Jun 01 '14 at 23:36
  • The [$c-1] part is because the preceding element is the actual index number I´m looking for in the BOTH array. – Tomaz Fernandes Jun 01 '14 at 23:42