I have a problem with my code. I want to get the first array from query and show it, after what show the next 2.
For the first Array in query I want to show img for the next 2 only the title
My code:
$link = mysql_connect('server', 'user', 'password')
or die("Can't");
$db = mysql_select_db('database')
or die ("can't");
$sql = 'SELECT * FROM News LIMIT 3';
$retval = mysql_query($sql,$link);
$filename ="footer.html";
$handle = fopen("footer.html", 'w+');
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//echo "<pre>"; print_r($row); echo "</pre>";
$content = '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<div class="block_articles">
<div class="block_header" >
<div class="headerlink" >
<a href="'.$sitelink.'" target="_blank"></div></div>';
$content .= '<img src="'.$sitelink.'news/'.$row['ImageName'].'" alt="'.htmlspecialchars($row['Title']).'" >';
$content .= ''.$row['Title'].'';
$content .= '<div class="date">';
$content .= $row['Date'];
$content .= '</div>';
$content .= '</div>';
echo $row['Title'].'<br>';
if ($handle)
{
if (!fwrite($handle, $content))
die("can't");
}
}
Based on your comments, you want to use a basic loop counter and change the behavior based on the counter:
theCurrentRow = 0;
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
theCurrentRow++;
if (theCurrentRow == 1) {
//show the images
}
else {
// show the title
}
}
Also to reiterate #AmalMurali's comment: don't use mysql_* functions, they're deprecated: http://php.net/manual/en/mysqlinfo.api.choosing.php
Related
I've been having issues displaying items that i saved from my database in an array. My table has two columns for 'name' and 'details'. I successfully retrieved them using mySQLi and stored in an array but instead of displaying all the columns at once, I want to place them at different places on my webpage. eg
<p>$Name1: $Details1</p>. then on another section of the same page
<p>$Name2: $Details2</p>. This is my code:
<?php
include ("config/database.php");
$mysqli = new mysqli($host, $user, $passwd, $database);
if ($mysqli-> connect_errno){
printf("Connect failed: %s\n",$mysqli-> connect_error);
exit();
}
$query = "SELECT name, details FROM recent_properties LIMIT 3";
if($result = $mysqli->query($query)){
printf("%s: %s<br>", $row["name"], $row["details"]);
$result->free();
}
$mysqli->close();
?>
Did you try like this.Make use of loop. To display all the records.
$query = "SELECT name, details FROM recent_properties LIMIT 3";
$result = $mysqli->query($query);
if($result->num_rows()>0)
{
while($row = $result->fetch_assoc())
{
printf("%s: %s<br>", $row["name"], $row["details"]);
}
}
$mysqli->close();
$query = "SELECT name, details FROM recent_properties";
$result = $mysqli->query($query);
if($result->num_rows()>0)
{
$html = "<table>";
$html .= '<tr><th>Name</th><th>Details</th></tr>';
while ($row = $result->fetch_assoc()) {
$html .= '<tr>';
$html .= '<td>' . $row["name"] . '<td>';
$html .= '<td>' . $row["details"] . '<td>';
$html .= '</tr>';
}
$html .= "</table>";
echo $html;
}
$mysqli->close();
this is the simplest way to create HTML content from data
in your case you can do some thing like this
if ($result->num_rows() > 0) {
$dataArray = [];
while ($row = $result->fetch_assoc()) {
$dataArray[$row["name"]] = $row["details"];
}
}
and then use the array in your HTML
<body>
<p><?php echo 'a: ' . $arrayData['a'] ?></p>
<p><?php echo 'b: ' . $arrayData['b'] ?></p>
<p><?php echo 'c: ' . $arrayData['c'] ?></p>
</body>
You need to loop through your $result and fetch an associative array:
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["name"], $row["details"]);
}
EDIT:
while ($row = $result->fetch_assoc()) {
echo '<p>'.$Name1.': '.$Details1.'</p>';
// Whatever else in this section
// You can even break out of PHP and then
// get back into PHP again if you have a lot of HTML.
}
I have separate question really which I need help. I only want to display say 20 characters from 'content'.
<?php
$output = '';
if(isset($_GET['q']) && $_GET['q'] !== ' ') {
$searchq = $_GET['q'];
$q = mysqli_query($db, "SELECT * FROM article WHERE title LIKE '%$searchq%' OR content LIKE '%$searchq%'") or die(mysqli_error());
$c = mysqli_num_rows($q);
if($c == 0) {
$output = 'No search results for <strong>"' . $searchq . '"</strong>';
} else {
while($row = mysqli_fetch_array($q)) {
$id = $row['id'];
$title = $row ['title'];
$content = $row ['content'];
$output .= '<a href="article.php?id=' .$id. '">
<h3>'.$title.'</h3></a>'.$content.'';
}
}
} else {
header("location: ./");
}
print("$output");
mysqli_close($db);
?>
i will answer your first question:
insert this line after:
$content = $row ['content'];
if(strlen($content)>20) $content=substr ($content,0,19);
I want to write in html file with PHP but is written only the last:
Please help me:
<?php
$link = mysql_connect('my_server', 'user', 'password')
or die("Can't");
$db = mysql_select_db('database')
or die ("Can't select");
$sql = 'SELECT * FROM News LIMIT 3';
$retval = mysql_query($sql,$link);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//echo "<pre>"; print_r($row); echo "</pre>";
$content = '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<div class="block">';
$content .=''.$row['Title'].'';
$content .='<div class="date">';
$content .=$row['Date'];
$content .='</div>';
$content .= '</div>';
$filename ="footer.html";
#unlink($filename);
$handle = fopen("footer.html", 'w+');
echo $row['Title'].'<br>';
if ($handle)
{
if (!fwrite($handle, $content))
die("cant' write");
}
}
?>
Write only the last query..
I tried but did not receive...
You need to put initial $content variable & file write part outside for loop.
$content = '';
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//echo "<pre>"; print_r($row); echo "</pre>";
$content .= '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<div class="block">';
$content .=''.$row['Title'].'';
$content .='<div class="date">';
$content .=$row['Date'];
$content .='</div>';
$content .= '</div>';
}
$filename ="footer.html";
#unlink($filename);
$handle = fopen("footer.html", 'w+');
echo $row['Title'].'<br>';
if ($handle)
{
if (!fwrite($handle, $content))
die("cant' write");
}
Currently I am using following code to get data sorted by starting letter of name, if you run this code you will get what i am trying to create
<?php
$dirs = array('Aname1','Aname2','Aname3','A Nmae','Bname ','Cname','Cardiff','Dname','Dname',);
$cur_let = null;
foreach ($dirs as $dir) {
if ($cur_let !== strtoupper(substr($dir,0,1))){
$cur_let = strtoupper(substr($dir,0,1));
echo "<li class=\"title\">".$cur_let."</li>";
}
echo "<li class=\"clear\">
<div class=\"name\">".$dir."</div>
<div class=\"mobile\"></div>
<div class=\"telephone\"></div>
<div class=\"email\"></div>
<div class=\"action\">edit | delete</div>
<div class=\"clear\"></div>
</li>";
}
but how to use above loop inside following to get vales from database and it should be display like (I want highlight first letter) http://i.stack.imgur.com/bLHVD.jpg
$query = "SELECT * FROM phone_number";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$names = $row['name'].",";
}
?>
With MySQLi:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = $mysqli->query($query);
while($row = $sql->fetch_assoc()) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
With mysql_* deprecated functions:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = mysql_query($query);
while($row = mysql_fetch_array($sql)) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
Try something like this:
$query = "SELECT * FROM phone_number ORDER BY name DESC";
$result = mysql_query($query) or die(mysql_error());
$lastLetter = '';
$html = '<ul>';
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
if (strtoupper($name[0]) !== $lastLetter) {
if ($lastLetter !== '')
$html .= '</ul></li>';
$lastLetter = strtoupper($name[0]);
$html .= '<li class="title">' . $lastLetter;
$html .= '<ul>';
}
$html .= '<li>' . $name . '</li>';
}
$html .= '</ul></li></ul>';
We have a basic PHP script to extract the title and description for each job from a MySQL database as simply display this information. This is what it looks like:
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
$results = mysql_fetch_assoc($query);
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$results['title']}</h2>";
echo "<p>{$results['desc']}</p>";
echo '</div>';
} ?>
Now, this only extracts one row from the database, but it should extract two. So, I tried the following to replace the while statement:
<?php foreach($results as $result) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
This statement doesn't work either. This just displays (weirdly) the first character of each column in the first row in the table.
Does anyone have any idea as to why this isn't working as it should?
In your while use same variable $result as you started:
while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
}
and remove the first $results = mysql_fetch_assoc($query);
Result variable you have used is result not results
Replace
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
**$results = mysql_fetch_assoc($query);** // remove this line
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$results['title']}</h2>";
echo "<p>{$results['desc']}</p>";
echo '</div>';
} ?>
to
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
You already fetched the first row before your loop started, which is why it only prints the second row. Simply comment out that line:
#$results = mysql_fetch_assoc($query); # here is your first row,
# simply comment this line
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
You are also looping over $result but using $results in your while loop body.
Check this:
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
<?php
while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
}
?>
Change this line
<?php while($result = mysql_fetch_assoc($query)) {
to
<?php while($results = mysql_fetch_assoc($query)) {