Can I Display Different Data Depending On What Row Number Is In Return, MYSQL?
$sqlCommand = "SELECT * FROM videos WHERE id='$pageid' ORDER BY id DESC LIMIT 4";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$id = $row["id"];
$titleCurVid = $row["title"];
$keywords = $row["keywords"];
$returnVariable = 'Hi, I am the first r开发者_Python百科eturn in the MYSQL select';
}
I want the $returnVariable to display different thing depending on what # return. for example. I want for the,
$returnVariable = 'Hi, I am the SECOND return in MYSQL';
to display that in the second returned row. I am sorry for my horrible explenation, I am just new to php and I don't know the terminology very well.
Nevermind, problem solved. I just did 4 different of these fetches.
1st Return (the one with the unique, special format display):
$sql_storyPublisher1 = mysql_query("SELECT * FROM story ORDER BY id DESC LIMIT 1"); while($row = mysql_fetch_array($sql_storyPublisher1)) { $publisherID1 = $row["id"]; $publishertitle1 = $row["title"]; $returnVariable1 = '<li id="selected" class="firstSlide YellowSelected">' . $publisherID1 . '</li>'; }
Thanks for the help everyone, I guess the simple solution what just using the OFFSET command.
2nd, 3rd & 4th Return (obviously replacing the number 2 with whatever # return it is):
$sql_storyPublisher2 = mysql_query("SELECT * FROM story ORDER BY id DESC LIMIT 1 OFFSET 1"); while($row = mysql_fetch_array($sql_storyPublisher2)) { $publisherID2 = $row["id"]; $publishertitle2 = $row["title"]; $returnVariable2 = '<li id="" class="secondSlide">' . $publisherID2 . '</li>'; }
You can create an array that stores the numbers (something like $arr = array(1 => "first", 2 => "second", 3 => "third", 4 => "fourth");
and so on
Then use a counter variable to access that array within the loop.
Here's what I'm thinking...
$sqlCommand = "SELECT * FROM videos WHERE id='$pageid' ORDER BY id DESC LIMIT 4";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$arr = array(1 => "first", 2 => "second", 3 => "third", 4 => "fourth", 5 => "fifth"); //and so on
$counter = 1
while ($row = mysqli_fetch_array($query)) {
$id = $row["id"];
$titleCurVid = $row["title"];
$keywords = $row["keywords"];
$returnVariable = 'Hi, I am the '.$arr[$counter].' return in the MYSQL select';
$counter++;
}
The syntax may be incorrect, but you get the idea. Note that the above example only shows it if there are 5 or less records.
Hope it makes sense.
You don't need to make four separate queries to the database... I meant something more like this:
///// 1st Return (the one with the unique, special format display) =
$sql_storyPublisher1 = mysql_query("SELECT * FROM story ORDER BY id DESC");
$row = mysql_fetch_array($sql_storyPublisher1);
$publisherID1 = $row["id"];
$publishertitle1 = $row["title"];
$returnVariable1 = '<li id="selected" class="firstSlide YellowSelected">' . $publisherID1 . '</li>';
///// 2nd, 3rd & 4th Return (obviously replacing the number 2 with whatever # return it is) =
$row = mysql_fetch_array($sql_storyPublisher2);
$publisherID2 = $row["id"];
$publishertitle2 = $row["title"];
$returnVariable2 = '<li id="" class="secondSlide">' . $publisherID2 . '</li>';
Of course there are probably better ways to do it, but this is simple and readable. You might want to make sure you'll actually have 4 rows. you can us if
if you're not sure:
if($row = mysql_fetch_array($sql_storyPublisher2)) {
$publisherID2 = $row["id"];
$publishertitle2 = $row["title"];
$returnVariable2 = '<li id="" class="secondSlide">' . $publisherID2 . '</li>';
}
精彩评论