<?php

ini_set("memory_limit","1024M");
set_time_limit(600000);

// connect import database

$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen.");
/*
$connection=mysql_connect('localhost', 'int-db-sql-shop', 'Msc7t24?381L') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('webshop-sql', $connection) or die("Konnte die Datenbank nicht waehlen.1");
*/
echo "get items";
$query = mysql_query("SELECT * FROM structure WHERE name!=''") or die("Anfrage nicht erfolgreich");
$data = array();
echo "set uris";
while($obj = mysql_fetch_object($query)) {
    //echo preg_replace("@[^0-9\-]@si", "", $number).'<br>';
        //$obj->name = utf8_encode($obj->name);
	$short_uri = $obj->name;
	$short_uri = str_replace(array('ä', 'Ä', 'ö', 'Ö', 'ü', 'Ü', 'ß'), array('ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss'), $short_uri);
	$short_uri = preg_replace("@[^0-9aA-zZ\-]@si", "-", $short_uri);
	$short_uri = preg_replace("@(-{2,})@si", "-", $short_uri);
	$new_uri = '';
	$old_uri = $short_uri;
	$i = 0;
	while ($new_uri == '') {
		$sql = "SELECT id FROM structure WHERE short_uri = '$short_uri' AND id != ".$obj->id;
		$result = mysql_query($sql);
		if (mysql_num_rows($result) == 0) {
			$new_uri = $short_uri;
		} else {
			$i++;
			$short_uri = $old_uri . '_' . $i;
		}
	}
	
	$sql = "
		UPDATE structure
		SET
                    short_uri='".$short_uri."'
		WHERE id=".$obj->id."
	";
	echo $sql.'<br>';
	$query2 = mysql_query($sql);
        if (!$query2) {
                echo $sql;
                echo "!!<br />";
        }
	
}
echo "ende";
?>