i want to make an news bar, by selecting mysql data and display it in and marquee, the problem that all the data are displayed at the same time in different lines, what i need is to display the data line by line.
code:
$news = mysql_query("SELECT ann_title, ann_text FROM o_postnews_conference");
while ($row = mysql_fetch_assoc($news)) {
echo "<marquee style='float:bottom;'><font color='snow'>{$row['ann_title']}: {$row['ann_text']}</font></marquee>";
It's because you're creating a new marquee element every iteration of your while loop. Use something like the below code instead.
$news = mysql_query("SELECT ann_title, ann_text FROM o_postnews_conference");
echo "<marquee style='float:bottom;'><font color='snow'>";
while ($row = mysql_fetch_assoc($news))
echo "{$row['ann_title']}: {$row['ann_text']} ";
echo "</font></marquee>";
Although you should know that marquee has been deprecated and you should be using CSS3/javascript instead.
Related
I'm building a PHP page, that return a list of items from my database. So I can't see any record because I think that the script go in overflow.
This is my script
<?php
ini_set('memory-limit','-1');
set_time_limit(0);
require_once('lib/connection.php');
$query_Articolo = "SELECT CodArticolo,NomeArticolo,Quantita,CodiceBarre, PrezzoAttuale, PrezzoRivenditore,PrezzoIngrosso
FROM VistaArticoli";
$result_Articoli = $connectiondb->query($query_Articolo);
//I HAVE JUST INSERT THIS LINE CODE
$answer[] =array();
while($row_Articoli = $result_Articoli->fetch_assoc()) {
$answer[] =array("id"=>$row_Articoli['CodArticolo'],"nome"=>$row_Articoli['NomeArticolo'],
"quantita"=>$row_Articoli['Quantita'],"codiceBarre"=>$row_Articoli['CodiceBarre']
,"codartFornitore"=>$row_Articoli['CodiceBarre'], "PrezzoAttuale"=>$row_Articoli['PrezzoAttuale'],
"prezzoRivenditore"=>$row_Articoli['prezzoRivenditore'],"prezzoIngrosso"=>$row_Articoli['prezzoIngrosso']);
}
//echo "risposta";
echo json_encode($answer);
?>
If I try tu call this page, I can't see any record. If I add at the query "limit 500" I can see the result.
How can I change the code?
I want to draw a graph where value will be taken from MySQL database. it is not working. But if I give the manual value then the graph is showing. Below is my code:
<?php
include "libchart/classes/libchart.php";
header("Content-type: image/png");
$chart = new LineChart();
$con=mysqli_connect("localhost","root","","bkash");
$result = mysqli_query($con,"SELECT time,trx_value FROM dialer_rate where mno='tnr'");
$serie1 = new XYDataSet();
while($row = mysqli_fetch_array($result))
{
$serie1->addPoint(new Point($row['time']->time, $row['trx_value']->trx_value));
}
$dataSet = new XYSeriesDataSet();
$dataSet->addSerie("TNR", $serie1);
$dataSet->addSerie("ROBI", $serie2);
$dataSet->addSerie("LNK", $serie3);
$dataSet->addSerie("AIR", $serie4);
$chart->setDataSet($dataSet);
$chart->setTitle("bKash Success/Failure for All MNO");
$chart->getPlot()->setGraphCaptionRatio(0.62);
$chart->render();
?>
i am not getting any value in time & trx_value in below line...
while($row = mysqli_fetch_array($result))
{
$serie1->addPoint(new Point($row['time']->time, $row['trx_value']->trx_value));
}
but if i want to print the value using echo below output comes
while($row = mysqli_fetch_array($result))
{ echo $row['time'];
echo $row['trx_value'];
};
Then output is coming..
also graph is showing when i am using manual value like below.
$serie1->addPoint(new Point("06-01", 273));
$serie1->addPoint(new Point("06-02", 421));
$serie1->addPoint(new Point("06-03", 642));
$serie1->addPoint(new Point("06-04", 799));
$serie1->addPoint(new Point("06-05", 1009));
$serie1->addPoint(new Point("06-06", 1106));
You've got quotes around $row['time']->time that shouldn't be there, and it looks like you're trying to "double-index" your columns (i.e. get values using both ['field'] and ->field).
new Point("$row['time']->time", $row['trx_value']->trx_value)
should probably be
new Point($row['time'], $row['trx_value'])
Notice how Stack Overflow's syntax highlighter suggests the first issue. If your text editor doesn't support syntax highlighting, it might be worthwhile to switch to one that does.
I am creating my own blog from scratch with a homepage that loads the latest posts in order of time published. I call the posts using a front controller and store the data on a MySQL database. The website itself is great and the posts all load perfectly with no issue. The issue is getting the homepage to work.
I created a few PHP functions for the homepage. They generally order the posts (database rows) by ID in descending order, since it's an autoincrement field, and call their data. And then to show the latest post as a sort of 'featured post' right at the top, by fetching the data from the very top row in the database, which is the latest post.
And that works fine - when I echo the result it shows the latest post just as I want it.
Below that I want two boxes, side by side, for the two posts before the first one. So I made this function to call them:
function fetch_rest_posts_1($conn) {
$stuff = $conn->query("SELECT * FROM table WHERE is_post = 1 ORDER BY id DESC LIMIT 1,2");
while ($row = $stuff->fetch_array()) {
$i=1;
return '<div id="post_'.$i.'" style="width:308px;height:215px;padding:5px">
<h2>'.$row['title'].'</h2>
<p>'.date('d/m/Y',strtotime($row['published_date'])).' by '.$row['author'].' | </p>
<p>'.$row['meta_description'].'</p>
</div>';
$i++;
} // style="white-space:nowrap;width:100%;overflow:hidden;text-overflow:ellipsis"
}
And it actually does work great when I echo the result, shows everything I want, but it only shows one div, not two. When I take the SQL query and directly enter it into phpMyAdmin, it gives me two rows. Have I done something wrong?
(I put the auto-increasing $i in there so that I could isolate each box and alter the style later.)
Your problem is caused by the return statement in the loop. You should add $return = '' at the top of your function, replace return by $result .=, and return $result at the end of your function.
In addition, the loop counter $i is reset in every iteration. Move the initial assignment out of the loop.
EDIT: The .= is intentional to append to $result instead of replacing it with another value constructed from the next dataset.
initiate $i outside the loop and use echo() instead of return()
return() breaks the loop
or use
$result .= '<div id="post_'.$i.'" style="width:308px;height:215px;padding:5px">
<h2>'.$row['title'].'</h2>
<p>'.date('d/m/Y',strtotime($row['published_date'])).' by '.$row['author'].' | </p>
<p>'.$row['meta_description'].'</p>
</div>';
and return $result; after the loop
That's because return will stop execution of the function try this approach:
function fetch_rest_posts_1($conn) {
$stuff = $conn->query("SELECT * FROM table WHERE is_post = 1 ORDER BY id DESC LIMIT 1,2");
$post = array();
while ($row = $stuff->fetch_array()) {
$post[] = $row;
}
return $post;
}
So the function purpose is to just get the data, so you can later print it:
$row = fetch_rest_posts_1($conn);
for($i = 0; count(row); $i++){
echo '<div id="post_'.$i.'" style="width:308px;height:215px;padding:5px">
<h2>'.$row[$i]['title'].'</h2>
<p>'.date('d/m/Y',strtotime($row['published_date'])).' by '.$row[$i]['author'].' | </p>
<p>'.$row[$i]['meta_description'].'</p>
</div>';
}
I have a SQLite database and am trying to graph data on a linechart using Highcharts, PHP, and Javascript. I am graphing one series per user (a user being a text value per tuple), but am running into trouble with retrieving a subsequent user via a PHP loop.
$dbhandle = sqlite_open('db/test.db', 0666, $error);
if (!$dbhandle) die ($error);
$query5 = "SELECT DISTINCT User FROM Events";
$ok0 = sqlite_query($dbhandle, $query5, $error_msg);
if (!$ok0)
{
die("dead" . $error_msg);
}
$rows = sqlite_num_rows($ok0);
echo
"for(var i=2; i<$rows; i++) // start of JS loop.
//$rows is 4; I am graphing 2 series here
{";
$array = sqlite_fetch_array($ok0, SQLITE_ASSOC); // $ok0 is the unique list
// of users. After graphing one series, I want to grab the next user to graph
echo "chart.addSeries({
name: '{$array["User"]}',
data: [";
for($i=0; $i<$diff+1; $i++)
{
$target = date("D, j M", (strtotime($_GET["start"]) + $i * 24 * 3600));
$query6 = "SELECT * FROM Events WHERE User = '{$array["User"]}' AND Start LIKE '%{$target}%'";
$result6 = sqlite_query($dbhandle, $query6);
if (!$result6) die("Cannot execute query.");
$num = sqlite_num_rows($result6);
if($i==($diff))
{
echo $num;
}
else
echo $num . ", ";
}?>],
pointStart:
<?php
$date = DateTime::createFromFormat('D M d Y', urldecode($_GET["start"]));
echo $date->getTimestamp()*1000;?>,
pointInterval: 24 * 3600 * 1000 // one day
});
<?php echo "}";?> // end of JS loop
The result does graph two additional series, but they are both the same data from the same user. It doesn't look like the loop with sqlite_fetch_array() correctly returns the next user. Can anyone see the problem here? Maybe something with the way I'm integrating Javascript with PHP?
This line of code always does the same thing every time you call it:
$query6 = "SELECT * FROM Events WHERE User = '{$array["User"]}' AND Start LIKE '%{$target}%'";
$array supposedly has a list of distinct users, but you don't show the query for $ok0. You're doing the user lookup inside this loop for($i=0; $i<$diff+1; $i++), but you use none of those variables in determining which user to fetch.
I'd like to post the correct way to look up the next user from $array, but I have no idea what the structure is. If you can't figure out how to make your $query6 unique to each user, you'll have to post more information, like a var_dump of $array or the query text of $ok0.
Maybe you just need to move this line $array = sqlite_fetch_array($ok0, SQLITE_ASSOC); inside your for loop, but then you run the risk of $i and $diff+1 disagreeing with the number of rows available from your result.
I've figured it out. I believe that due to PHP being executed server-side, the block of code within the echoed Javascript loop results in the same values when the browser executes the Javascript. The server simply echoes the string that represents the js loop, and will run the code block after. When everything is sent to the browser, the Javascript will loop normally and simply output the results of the code block twice. I've just forgone the JS loop via PHP and replaced it with a PHP loop.
well, i wanna pull out some data from a mysql view, but the wuery dos not seem to retrieve anything ( even though the view has data in it).
here is the code i've been "playing" with ( i'm using adodb for php)
$get_teachers=$db->Execute("select * from lecturer ");
//$array=array();
//fill array with teacher for each lesson
for($j=0;$j<$get_teachers->fetchrow();++$j){
/*$row2 = $get_lessons->fetchrow();
$row3=$row2[0];
$teach=array(array());
//array_push($teach, $row3);
$teach[$j]=mysql_fetch_array( $get_teachers, TYPE );
//echo $row3;*/
$row = $get_teachers->fetchrow();
//$name=$row[0]+" "+$row[0]+"/n";
//array_push($teach, $row1);
echo $row[0]; echo " ";echo $row[1]." ";
//$db->debug = true;
}
if i try something like "select name,surname from users", the query partially works . By partially i mean , while there are 2 users in the database, the loop only prints the last user.
the original query i wanted to execute was this
$get_teachers=$db->Execute("select surname,name from users,assigned_to,lessons
where users.UID=assigned_to.UID and lessons.LID=assigned_to.LID and
lessons.term='".$_GET['term']."'");
but because it didnt seem to do anything i tried with a view ( when you execute this in the phpmyadmin it works fine(by replacing the GET part with a number from 1 to 7 )
the tables in case you wonder are: users,assigned_to and lessons. ( assigned_to is a table connecting each user to a lesson he teaches by containing UID=userid and LID=lessonid ). What i wanted to do here is get the name+surname of the users who teach a lesson. Imagine a list tha displays each lesson+who teaches it based on the term that lesson is available.
Looking at http://adodb.sourceforge.net/ I can see an example on the first page on how to use the library:
$rs = $DB->Execute("select * from table where key=123");
while ($array = $rs->FetchRow()) {
print_r($array);
}
So, you should use:
while ($row = $get_teachers->fetchrow()) {
instead of:
for ($j = 0; $j < $get_teachers->fetchrow(); ++$j) {
The idea with FetchRow() is that it returns the next row in the sequence. It does not return the number of the last row, so you shouldn't use it as a condition in a for loop. You should call it every time you need the next row in the sequence, and, when there are no more rows, it will return false.
Also, take a look at the documentation for FetchRow().
for($j=0;$j<$get_teachers->fetchrow();++$j){
... a few lines later ...
$row = $get_teachers->fetchrow();
See how you call fetchrow() twice before actually printing anything? You remove two rows from the result set for every 1 you actually use.
while ($row = $get_teachers->fetchrow()) {
instead and don't call fetchrow() again within the loop.
Because you're fetching twice first in the loop
for($j=0;$j<$get_teachers->fetchrow();++$j){
... some code ...
// And here you fetch again
$row = $get_teachers->fetchrow();
You should use it like this
while ($row = $get_teachers->fetchrow()) {