I'm trying to make a tab for my page, where each tab shows a different field of a mysql table row. I'd like to switch contents in the tab. Every row has a different id, and I would like to make a tabbed design. The data should be chosen by the id of the table. The tab works fine if i use just a simple text in the echo part, but I can't fetch the data from my database. I tried this code, but it doesn't work.
<?php
if (isset($data[0]) && is_numeric($data[0]) ) {
$content = mysql_fetch_assoc ( mysql_query( "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name where id = $data[0];") );
} else {
$content = mysql_fetch_assoc ( mysql_query( "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name order by id asc limit 1;") );
}
switch($_GET['tabNum']) {
case 1: echo strip_tags($content['tab_content_one']); break;
case 2: echo strip_tags($content['tab_content_two']); break;
case 3: echo strip_tags($content['tab_content_three']); break;
case 4: echo strip_tags($content['tab_content_four']); break;
case 5: echo strip_tags($content['tab_content_five']); break;
}
?>
I don't know what's wrong with my code. Do you have any idea?
You've got to enclose the mysql_fetch_assoc into a while loop! For your code it will be something like this:
if (isset($data[0]) && is_numeric($data[0]) ) {
$sql = "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name where id = $data[0];";
} else {
$sql = "select id,tab_content_one,tab_content_two,tab_content_three,tab_content_four,tab_content_five from db_name order by id asc limit 1;";
}
$result = mysql_query($sql);
while($content = mysql_fetch_assoc($result)){
switch($_GET['tabNum']) {
case 1: echo strip_tags($content['tab_content_one']); break;
case 2: echo strip_tags($content['tab_content_two']); break;
case 3: echo strip_tags($content['tab_content_three']); break;
case 4: echo strip_tags($content['tab_content_four']); break;
case 5: echo strip_tags($content['tab_content_five']); break;
}
}
Anyway it's suggested to you as mysql_fetch_assoc it's going to be deprecated starting from php 5.5.0 to use the mysql_PDO extension instead.