Simple HTML Dom Parse Tables - php

I want to parse the news from the website which is : http://www.bakirkoykhb.gov.tr/?module=bizden_haberler , what i'm facing is the website has many tables without classes and very complicated and so on as you can see if you check the source code of the page, i get the headings like those ones :
Japonya-Türkiye Tıp Derneği'den Ziyaret
Saglık-Sen Gen. Başk.Memiş'ten Ziyaret
Vali Mutlu’ dan Ziyaret
and so on with my code below. but i don't have any idea that how can i parse the links which are contained under "Devamını Oku".
$html = file_get_html('http://www.bakirkoykhb.gov.tr/?module=bizden_haberler');
$j = 0;
foreach($html->find('b') as $element2){
echo "
<h3 ><a target=\"_blank\" href=\"#somethingtohere\">". $element2->plaintext . "</a></h3>
";
$j++;
if($j > 5) break;
}
Hope you can help, thank you.

This should do for you
echo "<h3 ><a href='http://www.bakirkoykhb.gov.tr/".$html->find('a',$j+22)->href."'>". $element2->plaintext . "</a></h3>";

Related

Making a 2 coloumn table with PHP without 'echoing' so much

I am working with PHP and My SQL to get data from an SQL database and show it in 2 columns.
I have figured out how to do it with the code as below. However I am working with a complex HTML template and don't want to echo every single HTML line to get it work - It will be very complicated to read and fix if there is an issue!
<table>
<?php
$i = 0;
while($row = $result->fetch_assoc()) {?>
<?php if (++$i % 2 != 0) echo "<tr>";?>
<?php echo "<td>" .$row['fname']."</td>"; ?>
<?php if ($i % 2 == 0);
} if ($i % 2 != 0); ?>
<td></td></tr>
</table>
I have simplified the template as below. Here I am 'echoing' within the HTML which I find easier to read and understand:
<div class=container>
<div id="fname"><a>First Name:</a><?php echo $row['fname'];?></div>
<div id="website"><a>Website:</a><?php echo $row['website'];?></div>
</div>
To summarize, is it possible to take my data from the database and show it in 2 columns without having to echo every single HTML line. Keeping my HTML structure similar to the above?
The column layout I am try to achieve is as below:
1 | 2
-----
3 | 4
-----
5 | 6
-----
I have not been able to find an example anywhere! Thanks!
There are advantages to writing HTML out in PHP script and there are also advantages to breaking into PHP from your HTML script. It really just depends on the circumstances.
For example if I am writing html that is generated from loops of has alot of variable inputs I will write the HTML inside the PHP. However, If I am writing a big chunk of HTML with only a few variables I will break into PHP from the HTML.
It really just depends on the circumstances.
That being said, when writing HTML from inside of PHP I will break it down and concatenate the HTML as much as possible. That way I am not breaking in and out of PHP to get the task accomplished. The more you code the easier that will get.
Below is how I would write your code. Notice I never broke in and out of PHP.
echo
'<table>';
$i = 0;
while($row = $result->fetch_assoc()) {
echo
'<tr>';
echo
'<td>' . $row['fname'] . '</td>' .
'<td>' . $row['lname'] . '</td>';
echo
'</tr>';
}
echo
'</table>';
Hope this helps.
You could do something like this in PHP, just another very simple option.
$str = '';
while($row = $result->fetch_assoc()) {
$str .= '<tr><td>'.$row['value1'].'</td><td>'.$row['value2'].'</td></tr>';
}
I don't know all the correct keys in the $row, but I think you get the idea.
And than the HTML would be like this:
<table>
<?php echo $str; ?>
</table>
Just a little hint. You can google on a MVC construction. This stands for Model View Controller and it is in my opinion the best way to keep your code organised.

PHP echo formatting of html

I am trying to echo out multiple table rows on my sql query and provide them with alternating colors to improve the aesthetic value of my site. I am fairly new to php and I am extremely fussy over the presentation of my code and therefore I would like to include the output html into my PHP block to improve readability.
I have browsed some past threads but I am still rather unclear of how the formatting of a string works in PHP, the code below shows my attempt of formatting the output:
echo '<tr class=" . 'if( $class_style %2 == 0 ){ echo "row_dark"; } else echo "row_light"' . ">';
What am I doing wrong here?
Regards
Alex.
You can't put an if structure in an echo.Use that :
echo '<tr class="'. ($class_style %2 == 0) ? 'row_dark' : 'row_light' . '">';
It's a ternary operation.
You should use like this right syntax:
echo '<tr class="'.($class_style %2 == 0 ? "row_dark" : "row_light").'">';
It should be
echo '<tr class=" '. instead of echo '<tr class=" .'
<?php echo '<tr class="'.(($class_style %2 == 0)?'row_dark':'row_light').'">';?>
or
<?='<tr class="'.(($class_style %2 == 0)?'row_dark':'row_light').'">';?>

Create a search box that search between id of divs

Well I’m kind of new in this with web programing and I’m sure I’m breaking some rules but I really need a hand here. Well the idea it’s that I have created a slider with the information that I bring from the data base, so I’m working with php and I’m making that a piece of code in php fills all the li and divs for the slider, and I’m giving the id to the div that contains all the details the name of the user of the data base, so the deal it’s that I have to have a search field that looks through all the divs I have, and after the search it only display the name of the user you are looking in the search field.
So this is the code I have of php that’s in the page, well I try to use an ajax but when the information comes from the php file it doesn’t respect the size of the div and prints all the information and I don’t get the slider.
<div class="slider" id="slider">
<?php
$cont = 0;
$bandera=0;
$tamano = 0;
$nombreUsuario = "";
$arrayIDs = array();
$i = 0;
$query_Total = "SELECT u.FOTO_HIJO_USUARIOS, u.ID_USUARIOS, u.NOMBRE_USUARIOS, u.NOMBRE_HIJO_USUARIOS, u.FB_ID_USUARIOS, m.ID_MOMENTOS_MAGICOS, m.TEXTO_MOMENTOS_MAGICOS FROM MOMENTOS_MAGICOS AS m, USUARIOS AS u WHERE u.ID_USUARIOS = m.ID_USUARIOS AND u.ESTADO_USUARIOS = '1'";
$todas_Total = mysql_query($query_Total, $MM_db) or die(mysql_error());
echo "<ul>";
while(($row = mysql_fetch_array($todas_Total, MYSQL_ASSOC))){
$nombreUsuario = $row['NOMBRE_USUARIOS'];
$arrayIDs[$i] = $nombreUsuario;
$i++;
$bandera++;
if($cont++%3==0){
echo '<li>';
}
echo '<div class="participantes" id="'.$nombreUsuario.'" style="float:left; padding-left:10px;width:145px;margin-right:25px;margin-left:15px; margin-top:15px">';
echo '<div class="face_foto"><img src="https://graph.facebook.com/'.$row['FB_ID_USUARIOS'].'/picture" width="52" height="52" /></div>';
echo '<h1 align="right">'.$nombreUsuario.'</h1>';
echo '<img class="bebe_participante" src="uploads/'.$row['FOTO_HIJO_USUARIOS'].'" width="153" height="89" />';
echo '<h2 align="center">'.$row['NOMBRE_HIJO_USUARIOS'].'</h2>';
echo '<div id="botMas"> <img class="img_votos" src="images/bt_ver_mas.png" width="89" height="35"/> </div>';
$query_Votos = "SELECT COUNT( * ) AS VOTOS FROM VOTOS WHERE ID_MOMENTOS_MAGICOS = '".$row['ID_MOMENTOS_MAGICOS']."'";
$todos_Votos = mysql_query($query_Votos, $MM_db) or die(mysql_error());
while(($row = mysql_fetch_array($todos_Votos, MYSQL_ASSOC))){
echo '<div class="votos">'.$row['VOTOS'].' VOTOS </div>';
}
echo '</div>';
$tamano++;
}
if($cont%3==0 || $cont==$bandera ){
echo '</li>';
}
echo "</ul>";
?>
</div>
So I was thinking of making the search with javascript or jquery, but after some tries I haven’t been able to make it.
So I really hope you guys can help me.
Thank you for your time :)
Use http://ivaynberg.github.com/select2/ for this.
See: Many-to-Many Ajax Forms (Symfony2 Forms) for an implementation (ignore the fact that this is tagged symfony2, it is relevant to php as well)

php simple concatenation problem

Here is a code sample of PHP function which prints HTML links. For some reason there is problem with the title attributte of the a tag(' games' isn't concatenated). For example if I have $gameCategorie = '3D' I get <a title='3D'>3D games</a> I want to get <a title='3D games'>3D games</a>
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title='.$gameContent.'>';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}
Ideas about improving the quality of the code and tutorials about HTML generation by PHP are also appreciated.
All valid xhtml should have attributes enclosed in speachmarks. Try this
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title="'.$gameContent.'">';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}

PHP/jQuery - Highlight the menu with current page

Hi
I am going to highlight a menu item according to the page that is reading currently, when user click on different page through the menu, that menu item will be highlighted, example is http://templates.joomlart.com/ja_pyrite/index.php?option=com_content&view=article&id=44&Itemid=53.
If I use PHP/jQuery to check the url and highlight the menu, it will be good if the url look like "http://example.com/contact", but the example above is bad.
If I don't going to check the url and highlight the menu item, could someone give me a idea/method that can be done with the same effect?
Thank you
I found this on 960 Development, been googling for a while for this, so happy when I finally found it!
<ul class="sub-nav" >
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
?>
<li><a class="<?php echo ($page_name=='where-to-buy.php')?'active':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
<li><a class="<?php echo ($page_name=='about.php')?'active':'';?>" href="about.php">ABOUT US</a></li>
<li><a class="<?php echo ($page_name=='contact.php')?'active':'';?>" href="contact.php">CONTACT US</a></li>
It is working fully for me, get the pages I need to be "active" to be active and dosent active any of thoes who got the class when I'm in another page!
Take a look at it!
Edit:
Even if you got a (in this example) contact.php?person=John, it will "active" the contact.php link!
do something like this
<div id="nav_menu">
<?php
$currentFile = $_SERVER['REQUEST_URI'];
$pages = array(
array("file" => "/index.php", "title" => "Home"),
array("file" => "/about.php", "title" => "About Us"),
array("file" => "/schedule.php", "title" => "Schedule")
);
$menuOutput = "<ul>";
foreach ($pages as $page) {
$activeAppend = ($page["file"] == $currentFile) ? " id='active' " : "class='nav_button'";
$currentAppend = ($page["file"] == $currentFile) ? " id='current' " : "class='nav_button'";
$menuOutput .= "<li " . $currentAppend . ">"
. "<a href='" . $page["file"] . "' id='".$page["id"]."'>" . $page["title"] ."</a>"
. "</li>";
}
$menuOutput .= "</ul>";
echo $menuOutput;
?>
</div>
i hope you get the idea, i had this on stackoverflow a while ago but i forgot what was the question
edit:
here i finnally found the original question
In the HTML code you use to generate your navigation, add some PHP logic that will add a selected class to the button of the page that you are currently on. Then just add some CSS for the selected class.
Can you do something like this? It should select any link that points at the current page - so you can apply whatever you like to highlight it.
$('a[href="'+window.location+'"]').addClass('menu-highlight');
A good technique is to add a specific class or id attribute to body element and then style it in CSS. It requires minimum of server side programming and keeps all the presentation logic in CSS as it should be done.
<style>
.contact #contact { background:#000; }
...
</style>
<body class="contact">
<ul>
<li id="homepage">Homepage</li>
...
<li id="contact">Contact</li>
</ul>
...

Categories